'The body might complete normally, causing 'null' to be returned, but the return type, 'State<StatefulWidget>', is a potentially non-nullable type
I'm new in flutter. I tried to build a webview that have a token. I have used statefull widget but I am facing this error The body might complete normally, causing 'null' to be returned, but the return type, 'State', is a potentially non-nullable type. The code:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
import 'package:maze/authentication/bloc/authentication_bloc.dart';
import 'package:maze/core/drawer.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../../authentication/bloc/authentication_state.dart';
import '../../core/secure_store.dart';
class ProfileView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
ProfileViewState createState() =>ProfileViewState();
}
}
class ProfileViewState extends State<ProfileView> {
build(BuildContext context) async {
var state = BlocProvider
.of<AuthenticationBloc>(context)
.state;
var token = await SecureStore().credentials;
final Completer<WebViewController> _controller =
Completer<WebViewController>();
return Scaffold(
appBar: AppBar(
title: const Text('Profile'),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
child: Text("name",
style: new TextStyle(fontWeight: FontWeight.bold)),
alignment: Alignment.bottomCenter,
),
),
],
),
drawer: const CustomDrawer(),
body: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
return Center(
child: WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController{
webViewController.loadUrl(
"http:exemple...",
headers: {"Authorization": "Bearer ${token}"});
_controller.complete(webViewController);
},
)
);
},
),
);
}
}
Solution 1:[1]
Change your createState method from this:
class ProfileView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
ProfileViewState createState() =>ProfileViewState();
}
}
To this:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() => ProfileViewState();
}
Solution 2:[2]
Update your profile view class like the below code:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() =>ProfileViewState();
}
Your have by mistake repeated the createState() line two times.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nonsocchi |
| Solution 2 | Ankit Kumar Maurya |
