首页 > 解决方案 > 如何将带有数据的数组转换为小部件

问题描述

如何将带有数据的数组转换为小部件

大批

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

}

我想去的页面代码

我想从数组中获取路径,然后替换它并转到页面。

标签: flutterdart

解决方案


从您的错误中可以看出,您正在从列表中接收字符串。

没有直接将字符串转换为小部件的方法,因此您必须通过比较手动检查从字符串中获得的内容,然后您可以从中创建小部件。

我希望以下最小的示例可以清除您的想法。

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

推荐阅读