【问题标题】:displaying selected data on Dropdownbutton on flutter在颤动的下拉按钮上显示选定的数据
【发布时间】:2019-12-18 14:52:30
【问题描述】:

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

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

我可以只在一次填充下拉菜单项(可能在 initstate 中),然后仅在需要时才请求刷新(即使用按钮)。我一直在尝试这样做,但一直失败。

这是我的代码

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

} 

【问题讨论】:

    标签: json flutter


    【解决方案1】:

    试试这个,

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

    【讨论】:

    • 它不起作用,我不太了解这部分_selectedChild == "" ? null : _selectedChild,。那应该是一个 if 语句吗?即便如此,我认为 if 语句在 value: 参数中不起作用
    • 它的三元运算符它的行为类似于 if else 语句
    猜你喜欢
    • 2020-10-05
    • 2021-11-21
    • 1970-01-01
    • 2021-11-05
    • 2020-12-16
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多