简体   繁体   English

在颤动的下拉按钮上显示选定的数据

[英]displaying selected data on Dropdownbutton on flutter

its actually continuation of my previous question , here i've been successfully populate my list with Json which i retrieved from Get method.它实际上是我上一个问题的延续,在这里我已经成功地用我从 Get 方法检索到的 Json 填充了我的列表。 as the suggestion, i used futurebuilder widget to build my Dropdownbutton.作为建议,我使用 futurebuilder 小部件来构建我的 Dropdownbutton。 but my problem is: it cant display any after i picked up the dropdownlist.但我的问题是:在我选择下拉列表后它无法显示任何内容。 it crashes.它崩溃了。

and i had other thing concerned me.我还有其他事情与我有关。 my project is to receive scanned nearby wifi by an esp8266 through http GET method (and later send the password through the apps using POST method).我的项目是通过 esp8266 通过 http GET 方法接收扫描的附近 wifi(然后使用 POST 方法通过应用程序发送密码)。 my question is: when im using this Futurebuilder widget, in which "time" that this app actually made http request?我的问题是:当我使用这个 Futurebuilder 小部件时,这个应用程序实际发出 http 请求的“时间”是什么? does it refresh all the time?会一直刷新吗?

can i just populate the dropdownmenuitem in just one occasion (perhaps in initstate), then asking for refresh only when i needed it (ie using a button).我可以只在一种情况下(可能在 initstate 中)填充下拉菜单项,然后仅在需要时才要求刷新(即使用按钮)。 i've been trying to do this but it kept failing.我一直在尝试这样做,但一直失败。

here's my code这是我的代码

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

} 

try this,尝试这个,

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

}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM