dart 在_WidgetsAppState中找不到路由RouteSettings(“/tests”,null)的生成器

yqkkidmi  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(100)

我创建了一个路由到另一个页面,当函数执行时,内容仍然为空,哪一部分是错误的?我还在学习
main.dart代码:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:nice_buttons/nice_buttons.dart';

void main() => runApp(MaterialApp(
  home: Main(),
));

class Main extends StatelessWidget {
  const Main({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "uas",
      home: Scaffold(
        body: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              transform: GradientRotation(108.7 * 3.14159 / 180),
              stops: [0.11, 0.882],
              colors: [
                Color.fromRGBO(221, 22, 224, 1),
                Color.fromRGBO(111, 22, 190, 1),
              ],
            ),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: Image.asset(
                  '../assets/Playloop_text.png',
                  height: 300,
                  width: 300,
                ),
              ),
              NiceButtons(
                stretch: false,
                progress: true,
                gradientOrientation: GradientOrientation.Horizontal,
                endColor:  Color.fromRGBO(221, 22, 224, 1),
                startColor: Color.fromRGBO(111, 22, 190, 1) ,
                borderColor: Color.fromRGBO(111, 22, 190, 1),
                borderThickness: 3,
                borderRadius: 20,
                onTap: (finish) {
                  print('berhasil');
                  Timer(Duration(seconds: 5), () {
                    Navigator.pushNamed(context, '/test');
                    finish();
                  });
                },
                child: Text(
                  'Next ->',
                  style: TextStyle(color: Colors.white, fontSize: 18),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

route.dart代码:

import 'package:flutter/material.dart';
import 'package:uas/main.dart';
import 'package:uas/menu.dart';

class RouteGenerator {
  static Route<dynamic> generateRoute(RouteSettings settings) {

    switch (settings.name) {
      case '/':
        return MaterialPageRoute(builder: (_) => Main());
      case '/test':
        return MaterialPageRoute(builder: (_) => MenuMusic());
      default:
        return _errorRoute();
    }
  }

  static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(builder: (_) {
      return Scaffold(
        appBar: AppBar(title: Text("Error")),
        body: Center(child: Text('Error page')),
      );
    });
  }
}

此错误消息:

Error: Could not find a generator for route RouteSettings("/tests", null) in the _WidgetsAppState.
]
lmyy7pcs

lmyy7pcs1#

你得到这个错误是因为你从来没有告诉Flutter使用你的方法来生成路线。
尝试将以下参数添加到MaterialApp构造函数:

onGenerateRoute: RouteGenerator.generateRoute

您必须首先将RouteGenerator类导入到应用中。

相关问题