Reputation: 1005
I'm trying to covert a passed String and convert it to a known List name.
I have a Map with a key value pair of string, string and I have 2 lists. The lists names are the held in the key field of the map. When I pass the the list name to my widget, I want it to show me the list contents, in list tiles, but I can't figure out how to convert that String to the list name. Here is my code:
I send on Line 71 and receive on Line 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]}'));
}),
],
),
));
}
}
Upvotes: 0
Views: 723
Reputation: 257
try to use map like this
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]}'));
}),
],
),
));
}
}
Upvotes: 0
Reputation: 4894
I would like to give you some idea. Try something like this,
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]}'),
);
},
),
],
),
),
);
}
}
or at the top level, like this,
// Below homes = {}
final Map detailedHomes = {
'home1': home1,
'home2': home2,
};
// Access it with detailedHomes['HOME NAME'][POSITION];
Hope that suits your case!
Upvotes: 1