从未来中返回列表

时间:2019-03-05 20:31:09

标签: android asynchronous dart flutter

嗨,我是flutter和异步编程的新手。我需要做这样的事情:

 public partial class DashBoard : Form
{
    List<User> user = new List<User>();
    public DashBoard()
    {
        InitializeComponent();
        UpdateBinding();
    }

    private void UpdateBinding()
    {

        UserFoundListBox.DataSource = user;
        UserFoundListBox.DisplayMember = "FullInfo";
    }

    private void SearchButton_Click(object sender, EventArgs e)
    {
        DataAccess db = new DataAccess();

        user = db.GetUser(LastNameText.Text);
        UpdateBinding();
    }

}

但是 <form method="post"> <select class='form-control' name= 'dropPic'> <option value="Flyer1pag1.png,Flyer1pag2.png">Flyer 1</option> <option value="Flyer2pag1.png,Flyer1pag2.png">Flyer 2</option> <option value="Flyer3pag1.png,Flyer1pag2.png">Flyer 3</option> </select> </form> <?php $dropPic=$_POST['dropPic']; $result=explode(',', $dropPic); $dropPic1=$result[0]; $dropPic2=$result[1]; ?> 返回List<Widget> usersProfiles = []; getUsers('DcofOiHWcjbjD0i18miW').then((user) { user.forEach((u) { usersProfiles.add(new ListTile( leading: CircleAvatar( backgroundImage: AssetImage(u.profilePicture), ), trailing: u.icon, title: new Text(u.name), onTap: () { Navigator.push(context, new MaterialPageRoute(builder: (context) => new Home())); }, )); }); });

我会非常感激的

3 个答案:

答案 0 :(得分:0)

如果要将其包装到返回Future的函数中,它将看起来像这样:

Future<List<Widget>> getUserProfiles() async {
  final List<Widget> userProfiles = [];
  user.forEach((u) {
    usersProfiles.add(new ListTile(
      leading: CircleAvatar(
        backgroundImage: AssetImage(u.profilePicture),
      ),
      trailing: u.icon,
      title: new Text(u.name),
      onTap: () {
        Navigator.push(context, new MaterialPageRoute(builder: (context) => new Home()));
      },
    ));
  });
  return userProfiles;
}

答案 1 :(得分:0)

我建议使用FutureBuilderStreamBuilder(用于流),它可以为您带来布局性能上的优势,还可以提供轻松添加加载和错误小部件的工具。 看起来可能如下:

Future<List<User>> usersFuture = getUsers('DcofOiHWcjbjD0i18miW');

将future创建为成员变量,因此您只需提取一次(以防每次调用该方法都会启动一个新的future)。然后在FutureBuilder中使用它。

FutureBuilder<User>(
    future: usersFuture,
    builder: (context, snapshot) {
      if(snapshot.connectionState != ConnectionState.done) {
        // return: show loading widget
      }
      if(snapshot.hasError) {
        // return: show error widget
      }
      List<User> users = snapshot.data ?? [];
      return ListView.builder(
        itemCount: users.length,
        itemBuilder: (context, index) {
          User user = users[index];
          return new ListTile(
            leading: CircleAvatar(
              backgroundImage: AssetImage(user.profilePicture),
            ),
            trailing: user.icon,
            title: new Text(user.name),
            onTap: () {
              Navigator.push(context,
                  new MaterialPageRoute(builder: (context) => new Home()));
            },
          );
      });
  });

答案 2 :(得分:-2)

Future<Solution> JuniorDeveloperQuestion {
// use Expansion List with Dynamic Data
return _2021.Solution.ExpansionListWithDynamicData; 
}

https://dev.to/rrtutors/dynamic-expansiontile-create-expansion-list-with-dynamic-data-2hl3