Flutter使用JSON填充dropdownmenu

编程入门 行业动态 更新时间:2024-10-28 20:24:44
本文介绍了Flutter使用JSON填充dropdownmenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从rest api返回的JSON并填充dropdownmenu,我需要将其值与子值不同.是否有人这样做或有示例或建议.我使用的是基本示例,似乎找不到使用JSON代替静态列表的方法.以下是我尝试过的方法,但仍然找不到该值.我不断收到飞镖null错误.任何帮助将是巨大的.我已经更新了如何获取JSON和进行解码.我已经尝试了几件事.这也是错误,但是我在调​​试中确认该值永远不会为null,因此它必须与JSON有关.我已经尝试过使用静态列表,并且可以使用.

I am trying to take JSON returned from rest api and populate a dropdownmenu, I need to have the value different from the child value. Has anyone done this or have an example or advice. I am using the basic example and can not seem to find a way to use JSON instead of static list. Below is what I have tried and it still does not find the value. I keep getting a dart null error. Any help would be great. I have updated with how I get the JSON and decode. I have tried several things. This is the error as well, but I have confirmed in debug that the value is never null, so it has to be something with the JSON. I have tried with a static list and it works.

'package:flutter/src/material/dropdown.dart': Failed assertion: line 433 pos 15: 'value == null String _referPractice = '<New>';

JSON看起来像这样:

JSON looks like this:

[{"id":0,"name":"<New>"},{"id":1,"name":"Test Practice"}] var http = createHttpClient(); var response = await http.get(_url); var practices = await jsonCodec.decode(response.body); practicelist = await practices.toList(); new DropdownButton<String>( value: _referPractice, isDense: true, onChanged: (String newValue) { setState(() { _referPractice = newValue; }); }, items: practicelist.map((value) { return new DropdownMenuItem<String>( value: value['id'].toString(), child: new Text(value['name']), ); }).toList(), ),

推荐答案

您收到此错误的原因是,您正在使用值为!null的值初始化_referPractice,并将其提供给该对象的属性value. DropDownButton,代表当前选中的项目,如果尚未选择任何项目,则必须为null.

You are getting this error because you are initializing _referPractice with a value that !null, and you are feeding it to the property value of the DropDownButton, which represents the currently selected item, and has to be null if no item has been selected yet.

我已使用您提供的JSON复制了您的示例:

I have replicated your example using the JSON you provided:

String _mySelection; List<Map> _myJson = [{"id":0,"name":"<New>"},{"id":1,"name":"Test Practice"}]; @override Widget build(BuildContext context) { return new Scaffold( body: new Center( child: new DropdownButton<String>( isDense: true, hint: new Text("Select"), value: _mySelection, onChanged: (String newValue) { setState(() { _mySelection = newValue; }); print (_mySelection); }, items: _myJson.map((Map map) { return new DropdownMenuItem<String>( value: map["id"].toString(), child: new Text( map["name"], ), ); }).toList(), ), ), ); }

更多推荐

Flutter使用JSON填充dropdownmenu

本文发布于:2023-11-03 09:16:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Flutter   JSON   dropdownmenu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!