尝试构建未来上下文,但我的构建方法有错误

Trying to build a future context but my build method has an error

我有这段代码,问题是: 'ProfileView.build' ('Future<Widget> Function(BuildContext)') 不是 'StatelessWidget.build' ('Widget Function(BuildContext)') 的有效覆盖。

我试图删除 future 但它不起作用,知道吗?

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);
                },
              
              )
          );
        },
      ),
    );
  }

}

我了解到您希望将构建方法设置为异步,因为您希望等待令牌初始化,但有更好的方法可以实现,这里是解决方案:

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);
                },
              )
          );
        },
      ),
    );
  }
}

阅读有关小部件生命周期的信息,它会帮助您首先了解出现错误的原因。