繁体   English   中英

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

[英]Dart / Flutter - How do I convert a passed string to a lists name?

我正在尝试转换传递的字符串并将其转换为已知的列表名称。

我有一个带有字符串键值对的 Map,字符串和我有 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]}'));
                  }),
            ],
          ),
        ));
  }
}

我想给你一些想法。 尝试这样的事情,

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];

希望适合您的情况!

尝试使用这样的地图

  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]}'));
              }),
        ],
      ),
      ));
      }
         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM