简体   繁体   中英

The body might complete normally, causing 'null' to be returned, but the return type, 'State<StatefulWidget>', is a potentially non-nullable type

I'm new in flutter. I tried to build a webview that have a token. I have used statefull widget but I am facing this error The body might complete normally, causing 'null' to be returned, but the return type, 'State', is a potentially non-nullable type. The code:

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

}

Update your profile view class like the below code:

    class ProfileView extends StatefulWidget {
     @override
     State<ProfileView> createState() =>ProfileViewState();

    }

Your have by mistake repeated the createState() line two times.

Change your createState method from this:

class ProfileView extends StatefulWidget {
 @override
 State<StatefulWidget> createState() {
 ProfileViewState createState() =>ProfileViewState();

 }

}

To this:

class ProfileView extends StatefulWidget {
 @override
 State<ProfileView> createState() => ProfileViewState();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM