我是 Flutter 的新手,我嘗試構建一個帶有令牌的 webview 我使用 statefull 小部件,但遇到了這個錯誤 主體可能正常完成,導致回傳“null”,但回傳型別“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);
},
)
);
},
),
);
}
}
uj5u.com熱心網友回復:
createState從此更改您的方法:
class ProfileView extends StatefulWidget {
@override
State<StatefulWidget> createState() {
ProfileViewState createState() =>ProfileViewState();
}
}
對此:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() => ProfileViewState();
}
uj5u.com熱心網友回復:
像下面的代碼一樣更新您的個人資料視圖類:
class ProfileView extends StatefulWidget {
@override
State<ProfileView> createState() =>ProfileViewState();
}
您錯誤地重復了 createState() 行兩次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467982.html
