Flutter Dart/FLIFT-如何将传递的字符串转换为列表名称?

Flutter Dart/FLIFT-如何将传递的字符串转换为列表名称?,flutter,dart,Flutter,Dart,我试图将传递的字符串转换为已知的列表名 我有一个带有一个键值对string的映射,string和2个列表。列表名称保存在地图的键字段中。当我将列表名传递给我的小部件时,我希望它以列表平铺的形式显示列表内容,但我不知道如何将该字符串转换为列表名。这是我的密码: 我在第71行发送,在第87行接收 import 'package:flutter/material.dart'; void main() { runApp(ListOfHomes()); } ////This is my list

我试图将传递的字符串转换为已知的列表名

我有一个带有一个键值对string的映射,string和2个列表。列表名称保存在地图的键字段中。当我将列表名传递给我的小部件时,我希望它以列表平铺的形式显示列表内容,但我不知道如何将该字符串转换为列表名。这是我的密码:

我在第71行发送,在第87行接收

import 'package:flutter/material.dart';

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

////This is my list of homes  this will even eventually come from firestore
Map<String, String> homes = {
  'home1': 'The one with the little yellow roof',
  'home2': 'The one with the outhouse',
};

//Home one has these rooms this will even eventually come from firestore
List<String> home1 = [
  'Room1',
  'Room2',
];

//Home2 has these rooms this will even eventually come from firestore
List<String> home2 = [
  'Room1',
  'Room2',
];

 
//This is OK for the moment

class ListOfHomes extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text('Home List'),
          ),
          body: SafeArea(
            child: Column(
              children: [
                ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: homes.length,
                    itemBuilder: (context, index) {
                      String key = homes.keys.elementAt(index);
                      return RowsWithData(roomDecor: key, roomName: homes[key]);
                    })
              ],
            ),
          ),
        ));
  }
}

class RowsWithData extends StatelessWidget {
  RowsWithData({this.roomDecor, this.roomName});

  final String roomName;
  final String roomDecor;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        FlatButton(
          onPressed: () {
            print(roomDecor);
            Navigator.push(
                context,
                MaterialPageRoute(
                  ///this is where my problem lies  I'm sending a String
                    builder: (context) => ListOfRooms(roomlist: roomDecor)));
          },
          child: ListTile(
            title: Text(roomDecor),
            subtitle: Text(roomName),
          ),
        ),
      ],
    );
  }
}


class ListOfRooms extends StatelessWidget {
  ///I'm receiving the string name here, but I need to return the corresponding List name
  ///
  final String roomlist;
  ListOfRooms({this.roomlist});


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('list of rooms'),
        ),
        body: SafeArea(
          child: Column(
            children: [
              ListView.builder(
                  itemCount: roomlist.length,
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  itemBuilder: (context, position) {
                    return ListTile(title: Text('${roomlist[position]}'));
                  }),
            ],
          ),
        ));
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(家庭列表());
}
////这是我的家庭名单,这甚至最终将来自firestore
地图主页={
“家1”:“有黄色小屋顶的那个”,
《家2》:“有厕所的那个”,
};
//Home one拥有这些房间,这些房间最终将来自firestore
列表home1=[
“1号房间”,
“2号房间”,
];
//Home2拥有这些房间,这些房间最终将来自firestore
列表home2=[
“1号房间”,
“2号房间”,
];
//目前还可以
类ListOfHomes扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“主列表”),
),
正文:安全区(
子:列(
儿童:[
ListView.builder(
滚动方向:轴垂直,
收缩膜:对,
itemCount:homes.length,
itemBuilder:(上下文,索引){
字符串键=homes.keys.elementAt(索引);
返回RowsWithData(roomDecor:key,roomName:homes[key]);
})
],
),
),
));
}
}
类RowsWithData扩展了无状态小部件{
RowsWithData({this.roomDecor,this.roomName});
最后一个字符串roomName;
最后的弦乐室装饰;
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
扁平按钮(
已按下:(){
印刷品(室内装饰);
导航器。推(
上下文
材料路线(
///这就是我的问题所在我在发送一个字符串
生成器:(上下文)=>ListofRoom(roomlist:roomDecor));
},
孩子:ListTile(
标题:文本(roomDecor),
字幕:文本(roomName),
),
),
],
);
}
}
类ListOfRooms扩展了无状态小部件{
///我在这里收到字符串名,但我需要返回相应的列表名
///
最终字符串列表;
listofRoom({this.roomlist});
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“房间列表”),
),
正文:安全区(
子:列(
儿童:[
ListView.builder(
itemCount:roomlist.length,
滚动方向:轴垂直,
收缩膜:对,
itemBuilder:(上下文、位置){
返回ListTile(标题:Text('${roomlist[position]}');
}),
],
),
));
}
}

我想给你一些想法。试试这样的

class ListOfRooms extends StatelessWidget {
  final Map homes = {
    'home1': ['Room1', 'Room2'],
    'home2': ['Room1', 'Room2'],
  };

  final String roomlist;
  ListOfRooms({this.roomlist});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('list of rooms'),
      ),
      body: SafeArea(
        child: Column(
          children: [
            ListView.builder(
              itemCount: homes[roomlist].length,
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemBuilder: (context, position) {
                return ListTile(
                  title: Text('${homes[roomlist][position]}'),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
// Below homes = {}

  final Map detailedHomes = {
    'home1': home1,
    'home2': home2,
  };
 
 // Access it with detailedHomes['HOME NAME'][POSITION];
或者在高层,像这样

class ListOfRooms extends StatelessWidget {
  final Map homes = {
    'home1': ['Room1', 'Room2'],
    'home2': ['Room1', 'Room2'],
  };

  final String roomlist;
  ListOfRooms({this.roomlist});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('list of rooms'),
      ),
      body: SafeArea(
        child: Column(
          children: [
            ListView.builder(
              itemCount: homes[roomlist].length,
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemBuilder: (context, position) {
                return ListTile(
                  title: Text('${homes[roomlist][position]}'),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
// Below homes = {}

  final Map detailedHomes = {
    'home1': home1,
    'home2': home2,
  };
 
 // Access it with detailedHomes['HOME NAME'][POSITION];

希望这适合你的情况

试着像这样使用map

  import 'package:flutter/material.dart';

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

 ////This is my list of homes  this will even eventually come from firestore
     Map<String, String> homes = {
    'home1': 'The one with the little yellow roof',
  'home2': 'The one with the outhouse',
    };

  //Home one has these rooms this will even eventually come from firestore
     List<String> home1 = [
  'Room1',
  'Room2',
   ];

//Home2 has these rooms this will even eventually come from firestore
    List<String> home2 = [
  'Room1',
  'Room2',
    ];


    //This is OK for the moment

    class ListOfHomes extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text('Home List'),
          ),
          body: SafeArea(
        child: Column(
          children: [
            ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: homes.length,
                itemBuilder: (context, index) {
                  String key = homes.keys.elementAt(index);
                  return RowsWithData(roomDecor: key, roomName: homes[key]);
                })
          ],
        ),
      ),
    ));
      }
    }

    class RowsWithData extends StatelessWidget {
      RowsWithData({this.roomDecor, this.roomName});

      final String roomName;
      final String roomDecor;

      Map<String,dynamic> map={
        'home1':home1,
        'home2':home2
      };

      @override
      Widget build(BuildContext context) {
        return Column(
        children: <Widget>[
       FlatButton(
      onPressed: () {
        Navigator.push(
            context,
            MaterialPageRoute(
              ///this is where my problem lies  I'm sending a String
                builder: (context) => ListOfRooms(roomlist: map[roomDecor])));
      },
      child: ListTile(
        title: Text(roomDecor),
        subtitle: Text(roomName),
      ),
        ),
      ],
    );
  }
    }


    class ListOfRooms extends StatelessWidget {
      ///I'm receiving the string name here, but I need to return the                                     corresponding List name
      final List<String> roomlist;
      ListOfRooms({this.roomlist});


      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
          title: Text('list of rooms'),
        ),
        body: SafeArea(
          child: Column(
            children: [
              ListView.builder(
              itemCount: roomlist.length,
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemBuilder: (context, position) {
                return ListTile(title: Text('${roomlist[position]}'));
              }),
        ],
      ),
      ));
      }
         }
导入“包装:颤振/材料.省道”;
void main(){
runApp(家庭列表());
}
////这是我的家庭名单,这甚至最终将来自firestore
地图主页={
“家1”:“有黄色小屋顶的那个”,
《家2》:“有厕所的那个”,
};
//Home one拥有这些房间,这些房间最终将来自firestore
列表home1=[
“1号房间”,
“2号房间”,
];
//Home2拥有这些房间,这些房间最终将来自firestore
列表home2=[
“1号房间”,
“2号房间”,
];
//目前还可以
类ListOfHomes扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“主列表”),
),
正文:安全区(
子:列(
儿童:[
ListView.builder(
滚动方向:轴垂直,
收缩膜:对,
itemCount:homes.length,
itemBuilder:(上下文,索引){
字符串键=homes.keys.elementAt(索引);
返回RowsWithData(roomDecor:key,roomName:homes[key]);
})
],
),
),
));
}
}
类RowsWithData扩展了无状态小部件{
RowsWithData({this.roomDecor,this.roomName});
最后一个字符串roomName;
最后的弦乐室装饰;
地图={
“home1”:home1,
“home2”:home2
};
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
扁平按钮(
已按下:(){
导航器。推(
上下文
材料路线(
///这就是我的问题所在我在发送一个字符串
builder:(context)=>listofRoom(roomlist:map[roomDecor]);
},
孩子:ListTile(
标题:文本(roomDecor),
字幕:文本(roomName),
),
),
],
);
}
}
类ListOfRooms扩展了无状态小部件{
///我在这里收到字符串名,但我需要返回相应的列表名
最终名单;
listofRoom({this.roomlist});
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“房间列表”),
),
正文:安全区(
子:列(
儿童:[
ListView.builder(
我