Navigator.push无法在flutter中使用setState

时间:2020-09-22 09:22:10

标签: flutter dart

Navigator.push无法与setState一起使用。

一旦我在retry()函数中删除了setState,它就可以了,但是我想在retry函数中使用setState。

import 'package:covid_19/screens/home.dart';
import 'package:covid_19/viewmodel/home_view_model.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyAppError());

class MyAppError extends StatefulWidget {
  @override
  _MyAppErrorState createState() => _MyAppErrorState();
}

class _MyAppErrorState extends State<MyAppError> {
  bool _loading = false;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return _loading ? CircularProgressIndicator() : MaterialApp(
      home: Scaffold(
          key: _scaffoldKey,
          body: Builder(
                builder:(context)=> 
              SafeArea(
                child: Container(
                  margin: EdgeInsets.all(10),
                  child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      "No internet found. Check your connection",
                      textAlign: TextAlign.center,
                    ),  
                    FlatButton(
                      child: Text('Retry'),
                      onPressed: retry,
                    )
                  ],
                ),
              ),
            ),
          ),
      )
    );
  }

  Future<void> retry() async{
    setState(() {
      _loading = true;
    });
    print('retrying...');
    HomeViewModel homeViewModel = HomeViewModel();
    homeViewModel.onAppStart().then((_){
      print('pushing');
      Navigator.pushReplacement(_scaffoldKey.currentContext, MaterialPageRoute(builder: (context)=> Home(data : homeViewModel.data)));
    }).catchError((e){
      setState(() {
        _loading = false;
      });
    });
    
  }
}

记录在没有setState的情况下单击重试按钮的时间:

retrying...
pushing... 
and navigate to Home()

使用setState单击重试按钮时记录:

retrying...
pushing... 
and nothing happens

1 个答案:

答案 0 :(得分:1)

它有一个逻辑错误。 _loading为true时,没有脚手架,并且您使用脚手架键进行导航。

尝试一下。

    import 'package:covid_19/screens/home.dart';
import 'package:covid_19/viewmodel/home_view_model.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyAppError());

class MyAppError extends StatefulWidget {
  @override
  _MyAppErrorState createState() => _MyAppErrorState();
}

class _MyAppErrorState extends State<MyAppError> {
  bool _loading = false;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          key: _scaffoldKey,
          body:_loading ? CircularProgressIndicator() : Builder(
              builder:(context)=> 
              SafeArea(
                child: Container(
                  margin: EdgeInsets.all(10),
                  child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      "No internet found. Check your connection",
                      textAlign: TextAlign.center,
                    ),  
                    FlatButton(
                      child: Text('Retry'),
                      onPressed: retry,
                    )
                  ],
                ),
              ),
            ),
          ),
      )
    );
  }

  Future<void> retry() async{
    setState(() {
      _loading = true;
    });
    print('retrying...');
    HomeViewModel homeViewModel = HomeViewModel();
    homeViewModel.onAppStart().then((_){
      print('pushing');
      Navigator.pushReplacement(_scaffoldKey.currentContext, MaterialPageRoute(builder: (context)=> Home(data : homeViewModel.data)));
    }).catchError((e){
      setState(() {
        _loading = false;
      });
    });
    
  }
}