错误:无法将参数类型“ Future <Widget>”分配给参数类型“ Widget”

时间:2020-07-30 09:16:33

标签: flutter dart

我看到其他人可以在他们的代码中执行此操作,但是为什么在尝试实现此操作时出现错误?

请帮助,因为我不知道发生了什么

public function getLatestList(){

$paginate = 10;

$data1 = DB::table('research_management')

->join('client','client.id','=','research_management.industry')

->select('research_management.*','client.image_link as image_link')

->where('delete_status','!=',2)

->orderBy('id','desc');

$data = DB::table('research_management1')

->join('client','client.id','=','research_management1.industry')

->select('research_management1.*','client.image_link as image_link')

->where('delete_status','!=',2)

->union($data1)

->orderBy('id','desc')->paginate($paginate);

return $data;

}

这是我对Future getLandingPage()的代码

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

出现的错误是这个

错误:参数类型'Future'不能分配给参数类型'Widget'。

更新 解决错误后,Future<Widget> getLandingPage() async { return StreamBuilder<FirebaseUser>( stream: _auth.onAuthStateChanged, builder: (BuildContext context, snapshot) { if (snapshot.hasData && (!snapshot.data.isAnonymous)) { return TransportMenu(); } return LoginPage(); }, ); } 中的另一个错误弹出,这是该错误的代码

TransportMenu

1 个答案:

答案 0 :(得分:0)

删除异步修改器和Future。在这种情况下您不需要它们

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

      return LoginPage();
    },
  );
}

还要从您的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(),
    );
  }
}