【问题标题】:Unhandled Exception: Could not find a generator for route RouteSettings("/CatPage", null) in the _WidgetsAppState未处理的异常:在 _WidgetsAppState 中找不到路由 RouteSettings("/CatPage", null) 的生成器
【发布时间】:2021-09-24 18:11:39
【问题描述】:

我是 Flutter 的新手,不明白为什么它在路由时给我一个错误,我的登录页面

class _LoginPageState extends State<LoginPage> {

  final _key = GlobalKey<FormState>();

  String uid = "";
  bool onLog = false;

  toCat() async{
    if(_key.currentState!.validate()){

      setState(() {
        onLog = true;
      });

      await Future.delayed(Duration(milliseconds: 1000));
      Navigator.pushNamed(context, MyRoutes.catPage);

      setState(() {
        onLog = false;
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
        child: Form(
          key: _key,
            child: Column(
              children: [
                SizedBox(height: 50,),
                Image.network('https://www.pngkit.com/png/full/6-60441_welcome-welcome-png.png', fit: BoxFit.cover,),
                SizedBox(height: 20,),
                Text('Hi, $uid', style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold),),
                SizedBox(height: 20,),
                Padding(
                  padding: EdgeInsets.all(30),
                  child: Column(
                    children: [
                      TextFormField(
                        decoration: InputDecoration(hintText: 'Enter username', labelText: 'Username'),
                        onChanged: (value){
                          uid = value;
                          setState(() {});
                        },
                      ),
                      TextFormField(
                        obscureText: true,
                        decoration: InputDecoration(hintText:'Enter password', labelText: 'Password'),
                        validator: (value) {
                          if(value!.isEmpty){
                            return 'password must not be empty';
                          }else if(value.length < 6){
                            return 'passowrd cannot be small';
                          }else{return null;}
                        },
                      ),
                      SizedBox(height: 25,),
                      InkWell(
                        onTap: (){toCat();},
                        child: AnimatedContainer(
                          alignment: Alignment.center,
                          duration: Duration(milliseconds: 300),
                          height: 50,
                          width: onLog ? 50 : 150,
                          decoration: BoxDecoration(color: Colors.cyan, borderRadius: BorderRadius.circular(onLog ? 50 : 8),),
                          child: onLog ? Icon(Icons.done) : Text('Login', style: TextStyle(color: Colors.black, fontSize: 20,),textScaleFactor: 1.2,),
                        ),

Inkwell小部件中的onTap功能,我想去这个页面

class _CatPageState extends State<CatPage> {
  Future<ModelClass> getImage() async {
    final Uri uri = Uri.parse("https://aws.random.cat/meow");
    final response = await (http.get(uri));

    if(response.statusCode == 200){
      final jsonData = jsonDecode(response.body);

      return ModelClass.fromJson(jsonData);
    }
    else{
      throw Exception;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: FutureBuilder<ModelClass>(future: getImage(),builder: (context, snapshot) {
        if (snapshot.hasData){
          final cat = snapshot.data;

          return Container(
            height: 400,
            width: 400,
            decoration: BoxDecoration(image: DecorationImage(image: NetworkImage(cat!.url), fit: BoxFit.cover,),),
          );
    }else if(snapshot.hasError){
          return Text(snapshot.error.toString());
        }
        return CircularProgressIndicator();
    }

标题中的错误或此错误

找不到路由 RouteSettings 的生成器

或者这个

使用不包含导航器的上下文请求导航器操作。

我无法理解路由

【问题讨论】:

  • 你是如何在MaterialApp 中定义路由的?
  • 没关系,这行得通: Nav(){ Navigator.push(context, MaterialPageRoute(builder: (context) => LoginForm()));设置状态(){}; }

标签: android ios flutter dart


【解决方案1】:

我设法解决了以下问题:

Nav()
{ Navigator.push(context, MaterialPageRoute(builder: (context) => LoginForm())); setState(){};
}

【讨论】:

    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2020-03-15
    相关资源
    最近更新 更多