[英]How do I use List data from list.dart file into main.dart
Below List is in list.dart file, I want to use this (suppose: 'place': 'Open Space',) data from this List to a Text() in my main.dart file.下面的列表在 list.dart 文件中,我想使用这个(假设:'place':'Open Space',)数据从这个列表到我的 main.dart 文件中的 Text() 。
List<Map<String, dynamic>> hotelList = [
{
'image': 'one.png',
'place': 'Open Space',
'destination': 'London',
'price': 25
},
{
'image': 'two.png',
'place': 'Global Will',
'destination': 'London',
'price': 40
},
{
'image': 'three.png',
'place': 'Tallest Building',
'destination': 'Dubai',
'price': 68
},
];
child: Row(
children: [
Text(
"???",
)
],
),
In your list.dart file you should put your list inside a class like this:在你的 list.dart 文件中,你应该将你的列表放在 class 中,如下所示:
class ListPlaces{
List<Map<String, dynamic>> hotelList = [
{
'image': 'one.png',
'place': 'Open Space',
'destination': 'London',
'price': 25
},
{
'image': 'two.png',
'place': 'Global Will',
'destination': 'London',
'price': 40
},
{
'image': 'three.png',
'place': 'Tallest Building',
'destination': 'Dubai',
'price': 68
},
];
}
Actually for something so small you cold just declare the list in your main, but lets say it is only the beginning of your app and you will keep using list.dart.实际上,对于这么小的东西,你只需要在你的 main 中声明列表,但可以说这只是你应用程序的开始,你将继续使用 list.dart。
In main.dart, you can then declare inside your main class "ListPlaces places" to create a variable that have access to the class content, like this:在 main.dart 中,您可以在 main class 中声明“ListPlaces places”以创建一个可以访问 class 内容的变量,如下所示:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
ListPlaces places;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Container(
child: Text(places.hotelList[0]['place']),
),
);
}
}
Note that you will have to use places.hotelList[0]['place'], 'places' is the name of your variable created with ListPlaces (you class name), 'hotelList' is the name of your list, '[0]' is the index of the location data in your list and '['place'] is the name of the field you want the content from.请注意,您必须使用 places.hotelList[0]['place'],'places' 是使用 ListPlaces 创建的变量的名称(您的 class 名称),'hotelList' 是您的列表名称,'[0 ]' 是列表中位置数据的索引,'['place'] 是您要从中获取内容的字段的名称。
Sorry if I could't make my self clear:(对不起,如果我不能说清楚:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.