【问题标题】:Is there any question about the route code in my flutter code?对我的颤振代码中的路由代码有任何疑问吗?
【发布时间】:2019-08-26 04:50:03
【问题描述】:

我想在flutter中开辟一条新路线,但我失败了, 我的 VS Code 给了我这个:

在处理手势时抛出以下断言: I/flutter (32582):使用不包含导航器的上下文请求导航器操作。 I/flutter (32582):用于从 Navigator 推送或弹出路由的上下文必须是小部件的上下文,该小部件是 I/flutter (32582):导航器小部件的后代

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Lake',
      routes: {
        'sss': (context)=>new NewRoute()
      },
      home: new Scaffold(
        appBar: AppBar(
          title: Text('Lake'),
        ),
        body: Text('BBB'),
        floatingActionButton: new FloatingActionButton(
          child: Icon(Icons.import_contacts),
          onPressed: (){
            Navigator.pushNamed(context, 'sss');
          },
        ),
      ),

    );
  }
}

class NewRoute extends StatelessWidget{
  @override
  Widget build(BuildContext context){
    return new Scaffold(
      appBar: AppBar(
        title: Text('BBB'),
      ),
      body: Center(
        child: Text('wahaha'),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter routes


    【解决方案1】:

    请使用此代码

    home: Builder(
            builder: (context) => Scaffold(
            appBar: AppBar(
              title: Text('Lake'),
            ),
            body: Text('BBB'),
            floatingActionButton: new FloatingActionButton(
              child: Icon(Icons.import_contacts),
              onPressed: (){
                Navigator.pushNamed(context, 'sss');
              },
            ),
          ),)
    

    Builder 让您可以像 https://docs.flutter.io/flutter/widgets/Builder-class.html 中描述的那样从直接父级构建新上下文

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 2022-01-21
      • 2021-11-28
      • 1970-01-01
      • 2022-11-05
      • 2019-12-23
      相关资源
      最近更新 更多