
[英]error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type
[英]The body might complete normally, causing 'null' to be returned, but the return type, 'State<StatefulWidget>', is a potentially non-nullable type
我是 flutter 的新手。我试图构建一个具有令牌的 webview。 我使用了 statefull 小部件,但我遇到了这个错误主体可能会正常完成,导致返回“null”,但返回类型“State”可能是不可为 null 的类型。 代码:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.