简体   繁体   English

如何将 list.dart 文件中的列表数据用于 main.dart

[英]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(
      "???",
    )
  ],
),

you can use following code您可以使用以下代码

Row(
      children: List.generate(
        hotelList.length,
        (index) {
          return Text('place : ${hotelList[index]['place']},');
        },
      ),
    )

在此处输入图像描述

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.

相关问题 如何在我的 main.dart 文件中使用 showChoiceDialog() - how can I use showChoiceDialog() in my main.dart file 如何在同一 lib 文件夹中的其他 screen.dart 文件中使用 main.dart 中的 flutter 变量? - How do i use flutter variables from main.dart in other screen.dart files in the same lib folder? 从 main.dart 调用另一个 dart 文件中的 dart 方法 - Calling a dart method in another dart file from main.dart 如何在main.dart中包含单独的文件 - How to include seperate file in the main.dart 从另一个 dart 文件调用 main.dart 中的 void function - Calling a void function in main.dart from another dart file 如何将 If 语句添加到 main.dart 文件 - How Add a If statement to main.dart file 如何将颤振包分离到另一个文件中,然后在 main.dart 中调用它? - How do I separate a flutter package into another file then call it in main.dart? 将数据从 custom_widget.dart 传递到 main.dart - Passing data from custom_widget.dart to main.dart 如何在我的 JS 文件中使用 main.dart 变量? - How can I use main.dart variable in my JS file? 如何将数据从根文件(main.dart)传递到flutter中的android广播接收器等不同的页面 - How to pass data from root file(main.dart) to different pages like android broadcast receiver in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM