Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 在颤振上的下拉按钮上显示所选数据_Json_Flutter - Fatal编程技术网

Json 在颤振上的下拉按钮上显示所选数据

Json 在颤振上的下拉按钮上显示所选数据,json,flutter,Json,Flutter,它实际上是我上一篇文章的延续,在这里,我已经成功地用我从Get方法检索到的Json填充了我的列表。根据建议,我使用futurebuilder小部件构建了我的下拉按钮。但我的问题是:在我拿起dropdownlist后,它无法显示任何内容。它崩溃了 我还有一件事和我有关。我的项目是通过http GET方法接收esp8266扫描的附近wifi(然后使用POST方法通过应用程序发送密码)。我的问题是: 当我使用这个Futurebuilder小部件时,这个应用程序实际发出http请求的“时间”是多少?它会

它实际上是我上一篇文章的延续,在这里,我已经成功地用我从Get方法检索到的Json填充了我的列表。根据建议,我使用futurebuilder小部件构建了我的下拉按钮。但我的问题是:在我拿起dropdownlist后,它无法显示任何内容。它崩溃了

我还有一件事和我有关。我的项目是通过http GET方法接收esp8266扫描的附近wifi(然后使用POST方法通过应用程序发送密码)。我的问题是: 当我使用这个Futurebuilder小部件时,这个应用程序实际发出http请求的“时间”是多少?它会一直刷新吗?

我是否可以只在一次情况下(可能在initstate中)填充dropdownmenuitem,然后仅在需要时请求刷新(即使用按钮)。我一直试图这样做,但一直失败

这是我的密码

Future<List<Post>> getAllPosts(String url) async {

  final response = await http.get(url);
  return allPostsFromJson(response.body);
}

List<Post> allPostsFromJson(String str) {
  final jsonData = json.decode(str);
  return new List<Post>.from(jsonData.map((x) => Post.fromJson(x)));
}

class Post {
  String name;
  String perc;
  String pass;

  Post({
    this.name,
    this.perc,
    this.pass,
  });

  factory Post.fromJson(Map<String, dynamic> json) => new Post(
        name: json["SSID"],
        perc: json["RSSI"],
        pass: json["PASS"],
      );

  Map<String, dynamic> toJson() => {
        "SSID": name,
        "RSSI": perc,
        "PASS": pass,
      };
}

class LoginPhaseState extends State<LoginPhase>{
  Post selected;

  final String uri = 'http://10.0.2.2/data/connection.json';


  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Post>>(
                future: getAllPosts(uri),
                builder: (BuildContext context,
                    AsyncSnapshot<List<Post>> snapshot) {
                  if (!snapshot.hasData) return CircularProgressIndicator();
                  return DropdownButton<Post>(
                    items: snapshot.data
                        .map((ssid) => DropdownMenuItem<Post>(
                              child: Text(ssid.name),
                              value: ssid,
                            ))
                        .toList(),
                    onChanged: (Post value) {
                      setState(() {
                        selected = value;
                      });
                    },
                    isExpanded: false,
                    // value: selected,
                    hint: Text('Select User'),
                  );
                });
  }

} 
Future getAllPosts(字符串url)异步{
最终响应=等待http.get(url);
返回allPostsFromJson(response.body);
}
列出allPostsFromJson(字符串str){
最终jsonData=json.decode(str);
返回新的List.from(jsonData.map((x)=>Post.fromJson(x));
}
班岗{
字符串名;
字符串perc;
串通;
职位({
这个名字,
这个.perc,,
这个,通行证,
});
factory Post.fromJson(映射json)=>新建Post(
名称:json[“SSID”],
perc:json[“RSSI”],
pass:json[“pass”],
);
映射到JSON()=>{
“SSID”:名称,
“RSSI”:perc,
“通过”:通过,
};
}
类LoginPhaseState扩展了状态{
选定职位;
最后一个字符串uri=http://10.0.2.2/data/connection.json';
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:getAllPosts(uri),
生成器:(BuildContext上下文,
异步快照(快照){
如果(!snapshot.hasData)返回CircularProgressIndicator();
返回下拉按钮(
项目:snapshot.data
.map((ssid)=>下拉菜单项(
子项:文本(ssid.name),
值:ssid,
))
.toList(),
一旦更改:(后值){
设置状态(){
所选=值;
});
},
isExpanded:错,
//值:选中,
提示:文本(“选择用户”),
);
});
}
} 
试试这个

Future<List<Post>> getAllPosts(String url) async {

  final response = await http.get(url);
  return allPostsFromJson(response.body);
}

List<Post> allPostsFromJson(String str) {
  final jsonData = json.decode(str);
  return new List<Post>.from(jsonData.map((x) => Post.fromJson(x)));
}

class Post {
  String name;
  String perc;
  String pass;

  Post({
    this.name,
    this.perc,
    this.pass,
  });

  factory Post.fromJson(Map<String, dynamic> json) => new Post(
        name: json["SSID"],
        perc: json["RSSI"],
        pass: json["PASS"],
      );

  Map<String, dynamic> toJson() => {
        "SSID": name,
        "RSSI": perc,
        "PASS": pass,
      };
}

class LoginPhaseState extends State<LoginPhase>{
  Post selected;

  final String uri = 'http://10.0.2.2/data/connection.json';

  String _selectedChild= "";

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Post>>(
                future: getAllPosts(uri),
                builder: (BuildContext context,
                    AsyncSnapshot<List<Post>> snapshot) {
                  if (!snapshot.hasData)
                   return                                          CircularProgressIndicator();
                  return DropdownButton<Post>(
                    value: _selectedChild == "" ? null : _selectedChild,
                    items: snapshot.data
                        .map((ssid) => DropdownMenuItem<Post>(
                              child: Text(ssid.name),
                              value: ssid,
                            ))
                        .toList(),
                    onChanged: (Post value) {
                      setState(() {
                         _selectedChild = value.name;
                        selected = value;
                      });
                    },
                    isExpanded: false,
                    // value: selected,
                    hint: Text('Select User'),
                  );
                });
  }

}  
Future getAllPosts(字符串url)异步{
最终响应=等待http.get(url);
返回allPostsFromJson(response.body);
}
列出allPostsFromJson(字符串str){
最终jsonData=json.decode(str);
返回新的List.from(jsonData.map((x)=>Post.fromJson(x));
}
班岗{
字符串名;
字符串perc;
串通;
职位({
这个名字,
这个.perc,,
这个,通行证,
});
factory Post.fromJson(映射json)=>新建Post(
名称:json[“SSID”],
perc:json[“RSSI”],
pass:json[“pass”],
);
映射到JSON()=>{
“SSID”:名称,
“RSSI”:perc,
“通过”:通过,
};
}
类LoginPhaseState扩展了状态{
选定职位;
最后一个字符串uri=http://10.0.2.2/data/connection.json';
字符串_selectedChild=“”;
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:getAllPosts(uri),
生成器:(BuildContext上下文,
异步快照(快照){
如果(!snapshot.hasData)
返回循环ProgressIndicator();
返回下拉按钮(
值:_selectedChild==“”?null:_selectedChild,
项目:snapshot.data
.map((ssid)=>下拉菜单项(
子项:文本(ssid.name),
值:ssid,
))
.toList(),
一旦更改:(后值){
设置状态(){
_selectedChild=value.name;
所选=值;
});
},
isExpanded:错,
//值:选中,
提示:文本(“选择用户”),
);
});
}
}  

它不起作用,我真的不理解这部分
\u selectedChild==“”?null:_selectedChild,
。这应该是一个if语句吗?即使如此,我也不认为if语句在value中起作用:parameterits的三元运算符它的行为类似于if-else语句