flutter - 如何将带有数据的数组转换为小部件
问题描述
如何将带有数据的数组转换为小部件
大批
[{id: 1, section_name: Name1, route: Gorod(), icon: Icons.location_city}, {id: 2, section_name: Name2, route: Gorod(), icon: Icons.chat}]
搜索数据
void SearchData() {
info = new List.from(data);
for (int i = 0; i < info.length; i++) {
Widget routed = info[i]['route'];
Navigator.push(context, MaterialPageRoute(builder: (context) => routed));
// Widget test = Gorod();
// Navigator.push(context, MaterialPageRoute(builder: (context) => test));
}
}
出现错误
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'Widget'
文件戈罗德();
import 'package:flutter/material.dart';
class Gorod extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return GorodState();
}
}
class GorodState extends State<Gorod> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData (
color: Colors.white,
),
title: Text('Title Gorod', style: TextStyle(color: Colors.white)),
),
body: Container (
child: Text('Text fdsf fds fdsf'),
)
);
}
}
我想去的页面代码
我想从数组中获取路径,然后替换它并转到页面。
解决方案
从您的错误中可以看出,您正在从列表中接收字符串。
没有直接将字符串转换为小部件的方法,因此您必须通过比较手动检查从字符串中获得的内容,然后您可以从中创建小部件。
我希望以下最小的示例可以清除您的想法。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var info = [
{
'id': 1,
'section_name': 'Name1',
'route': 'Gorod()',
'icon': 'Icons.location_city'
},
{
'id': '2',
'section_name': 'Name2',
'route': 'Gorod()',
'icon': 'Icons.chat'
}
];
List<Widget> searchData() {
List<Widget> _list = [];
for (int i = 0; i < info.length; i++) {
print(info[i]['route']);
if (info[i]['route'] == "Gorod()") {
_list.add(RaisedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Gorod()));
},
child: Text("text"),
));
}
}
return _list;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Column(
children: searchData(),
),
),
);
}
}
class Gorod extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return GorodState();
}
}
class GorodState extends State<Gorod> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.white,
),
title: Text('Title Gorod', style: TextStyle(color: Colors.white)),
),
body: Container(
child: Text('Text fdsf fds fdsf'),
));
}
}
推荐阅读
- python - 找到重复项时如何更新元组
- c++ - 在 MSDOS 编译器上初始化一个大于最大值的 int 时输出的混乱
- c# - 向默认 Index.cshtml RazorPages Asp.Net Core 2.1.0 添加一个属性
- java - Android facebook-sdk 在土耳其语中崩溃
- javascript - 点击时的 jQuery 手风琴事件
- c# - c# WPF image show tiff (just 1 page) blurry
- android - Appium 在第二个活动中找不到任何元素,如第一个活动所示
- java - jarsigner 在 java 中不起作用,因此构建失败
- gdal - gdallocationinfo 的插值结果?
- api-design - API 设计:传递 id 或完整对象