例如,
我在main.dart中有一个简单的Map()。

我有3个屏幕,其中包含不同的状态小部件,

screen1.dart => Screen1()

screen2.dart => Screen2()

screen3.dart => Screen3()

在main.dart中

var people1 = {

name:”Peter”,
age:20,

}
Main(){
  runApp(screen1(people1));
}

========================

screen1.dart

(导入的main.dart)
class Screen1 extends StatefulWidget {
  @override
  _Screen1State createState() => _Screen1State();
}


class _Screen1State extends State<Screen1> {

  @override
  Widget build(BuildContext context) {
    return Container(Text(“${people.age}”);
  }
}

我无法在其他屏幕上找到人员。甚至我也导入了dart文件。
如何在其他屏幕上转移到有状态的小部件?

最佳答案

您必须在Screen1中创建构造函数

class Screen1 extends StatefulWidget {
  final Map people;

  const Screen1(this.people);

然后像这样在全状态类Screen1中访问它
widget.people.age

如果您的Screen1将是无状态的,则可以通过
people.age

10-08 14:40