【问题标题】:Incorrect use of parentDataWidget in Flutter when loading json dataFlutter 中加载 json 数据时使用 parentDataWidget 不正确
【发布时间】:2021-10-21 18:45:30
【问题描述】:

为什么它告诉我不正确地使用 ParentDataWidget?我是否正确加载了我的 json 数据?我的目标是将我的 json 数据放入带有搜索栏的 ListView 中。加载我的数据时结构是否正确?实现我的搜索栏时它会起作用吗?我尝试过使用 FutureBuilder 的其他实现,但没有成功

尝试加载资产时出错:无法在“assets/assets/files/myfiles.json”加载资产 (404)

class MainPage3 extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<MainPage3> {
  List _items = [];
  List _meanings = [];
  List _examples = [];

  // Fetch content from the json file
  Future<void> readJson() async {
    final String response = await rootBundle.loadString('assets/files/myfiles.json');
    final data = await json.decode(response);
    setState(() {
      _items = data["VERB"];
      _meanings = data['MEANING'];
      _examples = data['EXAMPLE'];
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          'Kindacode.com',
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.all(25),
        child: Column(
          children: [
            ElevatedButton(
              child: Text('Load Data'),
              onPressed: readJson,
            ),

            // Display the data loaded from sample.json
            _items.length > 0
                ? Expanded(
                    child: ListView.builder(
                      itemCount: _items.length,
                      itemBuilder: (context, index) {
                        return Card(
                          margin: EdgeInsets.all(10),
                          child: ListTile(
                            leading: Text(_items[index]["VERB"]),
                            title: Text(_meanings[index]["MEANING"]),
                            subtitle: Text(_examples[index]["EXAMPLE"]),
                          ),
                        );
                      },
                    ),
                  )
                : Container()
          ],
        ),
      ),
    );
  }
}

我的json数据是这样的:

{"VERB": "ASK  OUT", "MEANING": "To invite on a date", "EXAMPLE": "Martin invited Carol out to dinner on Sunday"},

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    删除ListView.builder 上方的Expanded 小部件,您就可以开始使用了。为什么? ListView 自动占用所有可用空间,就像 Expanded 小部件所做的那样,因此删除它即可解决问题。还要确保将 shrinkWrap 设置为 true。

    ListView.builder(
      shrinkWrap: true,
      ...
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 2021-09-27
      • 2019-11-24
      • 2021-01-22
      • 1970-01-01
      相关资源
      最近更新 更多