正文可能会正常完成,导致 'null' 被 returned,但是 return 类型 'State<StatefulWidget>' 是潜在的不可空类型
The body might complete normally, causing 'null' to be returned, but the return type, 'State<StatefulWidget>', is a potentially non-nullable type
我是 flutter 的新手。我试图构建一个具有令牌的 webview。我使用了有状态的小部件,但我遇到了这个错误
正文可能会正常完成,导致 'null' 被 returned,但是 return 类型 'State' 可能不是- 可空类型。
代码:
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);
},
)
);
},
),
);
}
}
像下面的代码一样更新您的个人资料视图class:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() =>ProfileViewState();
}
您错误地将 createState() 行重复了两次。
改变你的createState
方法:
class ProfileView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
ProfileViewState createState() =>ProfileViewState();
}
}
为此:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() => ProfileViewState();
}
我是 flutter 的新手。我试图构建一个具有令牌的 webview。我使用了有状态的小部件,但我遇到了这个错误 正文可能会正常完成,导致 'null' 被 returned,但是 return 类型 'State' 可能不是- 可空类型。 代码:
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);
},
)
);
},
),
);
}
}
像下面的代码一样更新您的个人资料视图class:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() =>ProfileViewState();
}
您错误地将 createState() 行重复了两次。
改变你的createState
方法:
class ProfileView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
ProfileViewState createState() =>ProfileViewState();
}
}
为此:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() => ProfileViewState();
}