传递到另一个屏幕的原始参数



我是新手,试图弄清楚为什么通过导航器将列表传递到另一个屏幕会修改原始列表。

我需要的是,在下一个屏幕上从_otherScreenList递减时不要修改主页列表。

我举了一个例子来演示正在发生的事情。 主页列表正在修改,即使我只对_otherScreenList进行更改。

单击 FAB 加号按钮以添加到主页列表,然后单击下一个屏幕。然后单击 FAB 减号按钮,它会修改 widget.passedInList 以及上一个屏幕 homePageList。

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 MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<String> homePageList = List();
void _incrementCounter() {
setState(() {
_counter++;
homePageList.add('A' + _counter.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo Home Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'homePageList',
),
Text(
'$homePageList',
style: Theme.of(context).textTheme.display1,
),
FlatButton(
color: Colors.blue,
child: Text('Next Screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => OtherScreen(
passedInList: homePageList,
),
),
);
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class OtherScreen extends StatefulWidget {
OtherScreen({Key key, this.passedInList}) : super(key: key);
final List<String> passedInList;
@override
_OtherScreenState createState() => _OtherScreenState();
}
class _OtherScreenState extends State<OtherScreen> {
List<String> _otherScreenList;
@override
void initState() {
super.initState();
_otherScreenList = widget.passedInList;
}
void _removeLast() {
setState(() {
// only remove from _otherScreenList, but it still removes from the widget.passedInList
_otherScreenList.removeLast();
print('widget.passedInList: ' + widget.passedInList.toString());
print('_otherScreenList: ' + _otherScreenList.toString());
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Other Screen'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'widget.passedInList',
),
Text(
'${widget.passedInList}',
style: Theme.of(context).textTheme.display1,
),
Text(
'otherScreenList',
),
Text(
'$_otherScreenList',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _removeLast,
tooltip: 'Remove last',
child: Icon(Icons.minimize),
),
);
}
}

此行为是有意义的,因为您正在传递对列表的引用。

但是,您希望homePageListotherPageList是两个不同的列表,以便使用homePageList的值初始化otherPageList

克隆列表的一种方法是使用List.from

class _OtherScreenState extends State<OtherScreen> {
List<String> _otherScreenList;
@override
void initState() {
super.initState();
_otherScreenList = List<String>.from(widget.passedInList);
}
// ...

现在,这将按您的预期工作。在这里你可以运行它:https://dartpad.dev/embed-flutter.html?id=a9f55b6de67133a9d686b389ad07da22&theme=dark

你不需要用Stringintbool等来做到这一点,因为Dart会将它们视为"原语"(即使它们是真实的对象(,并通过值将它们传递给方法和构造函数,而不是通过引用,这绝对是更复杂的数据结构(如List(的情况。

最新更新