【问题标题】:Parameter is null instead of an id with ModalRoute.of()参数为 null 而不是 ModalRoute.of() 的 id
【发布时间】:2021-03-27 17:59:50
【问题描述】:

我正在使用 Flutter 和 Firebase 开发移动应用。

我正在尝试使用 pushNamed() 并交出一个参数。 (身份证)

我不知道如何解决我的问题。

这是我的代码:

@override
  void didChangeDependencies() {
    if (_isInit) {
      print(ModalRoute.of(context).settings.arguments);
      final productId = ModalRoute.of(context).settings.arguments;
      if (productId != null) {
        _editedAngebot = Provider.of<Angebote>(context).findByID(productId);
        _initValues = {
          'titel': _editedAngebot.titel,
          'beschreibung': _editedAngebot.beschreibung,
          'semester': _editedAngebot.semester.toString(),
          'fach': _editedAngebot.fach,
          'abteilung': _editedAngebot.abteilung,
        };
      }
    }
    _isInit = false;
    super.didChangeDependencies();
  }

还有另一个类,我在其中设置了参数。我的“Angebot”对象只有一个默认构造函数。

trailing: isAllowed()
            ? IconButton(
                icon: Icon(Icons.edit),
                onPressed: () {
                  Navigator.of(context).maybePop();
                  Navigator.of(context)
                      .pushNamed('/editAngebot', arguments: id);
                })

为什么我的 ID 为空?

【问题讨论】:

  • 使用这个:onPressed: (){ Navigator.of(context).pushReplacementNamed(Secondpage.routeName,arguments: id); },

标签: firebase flutter null screen navigator


【解决方案1】:

您的 Id 为空,因为您先弹出一个页面,然后再推送新页面。

使用 pushReplacementNamed()

这是一个代码示例

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: FirstPage(),
      routes:{
        Secondpage.routeName:(context)=>Secondpage(),
      }
    );
  }
}

class FirstPage extends StatelessWidget {
  final String id = '01';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child:ElevatedButton(
        child: Text('GoTo secondPage'),
        onPressed: (){
          Navigator.of(context).pushReplacementNamed(Secondpage.routeName,arguments: id);
        },
      ))
    );
  }
}

class Secondpage extends StatelessWidget {
  static const routeName = 'secondpage';
  @override
  Widget build(BuildContext context) {
    final data = ModalRoute.of(context).settings.arguments as String;
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Center(child:Text('$data')),
          ElevatedButton(
        child: Text('GoTo FirstPage'),
        onPressed: (){
          Navigator.of(context).pop();
        },
      )
        ],
      )
    );
  }
}

【讨论】:

    【解决方案2】:

    您可能应该将类中定义的 id 更改为动态类型,它会起作用.. 对此进行了测试,它可以正常工作

    class Product with ChangeNotifier {
      //changed to dynamic as errors with null and string came topping up
    
      final dynamic id;
      final String title;
      final String description;
     
    
      Product(
          {required this.id,
          required this.title,
          required this.description,
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2022-07-11
      • 1970-01-01
      相关资源
      最近更新 更多