我有這段代碼,問題是:
'ProfileView.build' ('Future<Widget> Function(BuildContext)')不是'StatelessWidget.build' ('Widget Function(BuildContext)').
我試圖洗掉未來,但它不起作用,知道嗎?
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 StatelessWidget {
const ProfileView({Key? key}) : super(key: key);
@override
Future<Widget> 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);
},
)
);
},
),
);
}
}
uj5u.com熱心網友回復:
我知道您希望將構建方法設定為異步,因為您想等待令牌初始化,但有更好的方法來實作這一點,這里是解決方案:
import 'package:flutter/material.dart';
class ProfileView extends StatefulWidget {
const ProfileView({Key? key}) : super(key: key);
@override
_ProfileViewState createState() => _ProfileViewState();
}
class _ProfileViewState extends State<ProfileView> {
var state;
var token;
@override
void initState() {
super.initState();
state = BlocProvider
.of<AuthenticationBloc>(context)
.state;
initToken();
}
initToken() async {
token = await SecureStore().credentials;
}
@override
Widget build(BuildContext context) {
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);
},
)
);
},
),
);
}
}
閱讀小部件生命周期,它將幫助您了解為什么首先出現錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467983.html
上一篇:主體可能正常完成,導致回傳“null”,但回傳型別“State<StatefulWidget>”可能是不可為空的型別
