简体   繁体   中英

Error: The argument type 'Future<Widget>' can't be assigned to the parameter type 'Widget'

I saw other people is able to do this in their code but why am i having an error when i try to implement this?

Please help as i do not know what is going on

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      home: await getLandingPage(),
    );
  }
}

this is my code for the Future getLandingPage()

Future<Widget> getLandingPage() async {
  return StreamBuilder<FirebaseUser>(
    stream: _auth.onAuthStateChanged,
    builder: (BuildContext context, snapshot) {
      if (snapshot.hasData && (!snapshot.data.isAnonymous)) {
        return TransportMenu();
      }

      return LoginPage();
    },
  );
}

the error that came out is this

Error: The argument type 'Future' can't be assigned to the parameter type 'Widget'.

UPDATE After fixing the error, another error in TransportMenu popped, this is the code for that

import 'package:flutter/material.dart';
import 'package:transportdemo/main.dart';
import 'package:transportdemo/personal_info.dart';
import 'package:transportdemo/sign_in.dart';
import 'login_page.dart';
import 'cluster_list.dart';
import 'personal_info.dart';

String cluster = 'HEART';
String preselected = 'HEART';
final listOfClusters = ['HEART', 'VOICE', 'MOVE', 'MIND', 'FORCE', 'STRIKE'];

class TransportMenu extends StatefulWidget {
  @override
  _TransportMenuState createState() => _TransportMenuState();
}

bool theme = true;

class _TransportMenuState extends State<TransportMenu> {
  _displayDialog(BuildContext context) async {
    return showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text('Select Your Cluster'),
            content: DropdownButtonFormField(
              value: preselected,
              icon: Icon(Icons.arrow_downward),
              decoration: InputDecoration(
                      labelText: 'Clusters',
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(10.0)),
                    ),
              items: listOfClusters.map((String value) {
                      return new DropdownMenuItem<String>(
                        value: value,
                        child: new Text(value),
                      );
                    }).toList(),
              onChanged: (String newValue) {
                setState(() {
                  preselected = newValue;
                cluster = newValue;
                });
              },validator: (value) {
                      if (value.isEmpty) {
                        return 'Please Select a cluster';
                      }
                      return null;
                    },
              isExpanded: false,
             ),
            actions: <Widget>[
              new FlatButton(
                child: Text('CONFIRM'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  @override
  void initState() {
    _getThingsOnStartup().then((value) {
      _displayDialog(context);
    });
    super.initState();
  }

  Future _getThingsOnStartup() async {
    await Future;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: theme ? ThemeData.light() : ThemeData.dark(),
        home: DefaultTabController(
            length: 3,
            child: Scaffold(
                appBar: AppBar(
                  actions: <Widget>[
                    Switch(
                      activeColor: Colors.white,
                      value: theme,
                      onChanged: (value) {
                        setState(() {
                          theme = value;
                        });
                      },
                    )
                  ],
                  title: Text('Welcome, ' +
                      name +
                      " " +
                      role +
                      '\nTo CYC Kuchai Transport App'),
                  automaticallyImplyLeading: false,
                  bottom: TabBar(
                    tabs: [
                      Tab(
                        text: 'Personal Info',
                        icon: Icon(Icons.person),
                      ),
                      Tab(
                        text: 'Transport\n  Request',
                        icon: Icon(Icons.directions_car),
                      ),
                      Tab(
                        text: 'Settings',
                        icon: Icon(Icons.settings),
                      )
                    ],
                  ),
                ),
                body: TabBarView(
                  children: [PersonalInfo(), ClusterMenu(), MyApp()],
                ))));
  }
}


Remove the async modifier and the Future. you do not need them in this case

Widget getLandingPage() {
  return StreamBuilder<FirebaseUser>(
    stream: _auth.onAuthStateChanged,
    builder: (BuildContext context, snapshot) {
      if (snapshot.hasData && (!snapshot.data.isAnonymous)) {
        return TransportMenu();
      }

      return LoginPage();
    },
  );
}

also, remove the await from your MaterialApp

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      home: getLandingPage(),
    );
  }
}

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