【问题标题】:Flutter how to update ListView in FutureBuilderFlutter 如何在 FutureBuilder 中更新 ListView
【发布时间】:2020-09-28 08:16:44
【问题描述】:

我想更新在 futurebuilder 中创建的列表视图,我想在滑块更改时更新它

FlutterSlider(
          values: [150],
          min: 0,
          max: 150,

          onDragging: (handlerIndex, lowerValue, upperValue) {
            radius = upperValue;
            users.clear();
            fut = _gdb.getUsersInRadius(radius);
          },
        ),

futurebuilder 和列表视图的代码

Expanded(
          child: FutureBuilder(
            future: fut,
            builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
              if(!snapshot.hasData && snapshot.connectionState == ConnectionState.done){
                return Center(
                    child: Text("No users"));
                   }else if(snapshot.connectionState == ConnectionState.waiting){
                return SpinKitWave(color: Theme.of(context).primaryColor);
              }
              return StreamBuilder(
                stream: snapshot.data,
                builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                  if(!snapshot.hasData){
                    return Center(
                        child: SpinKitWave(
                          color: Theme.of(context).primaryColor,
                        ));
                       }else{
                         print(snapshot.data.length);
                         users.addAll(snapshot.data);
                         return ListView.builder(
                             controller: _scrollController ,
                             itemCount: users.length,
                             itemBuilder: (BuildContext ctx, int index){

                                 return GestureDetector(
                                   onTap: (){
                                    Navigator.pushNamed(context, '/userWidget', arguments: users[index]);
                                   },
                                   child: Card(
                                     child: Column(
                                       children: [
                                         Text(users[index].data['username']),
                                         FutureBuilder(
                                           future: FirebaseStorage.instance.ref().child('profilepics').child('${users[index].documentID}.jpg').getDownloadURL(),
                                           builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                                             if(!snapshot.hasData){
                                               return CircleAvatar();
                                             }
                                             return CircleAvatar(backgroundImage: NetworkImage(snapshot.data) ,) ;//.network(snapshot.data)
                                           },
                                         ),
                                         Text(users[index].data['city'] == null ? "No city" : users[index].data['city'] )
                                       ],
                                     ),
                                   ),
                                 );
                             });
                       }
                     },
                   );
                 },
               ),
             )

当用户更改滑块的值时,附近的新用户将被加载到列表视图,旧用户将被删除 使用 Geoflutterfire 从 Firestore 下载用户,

【问题讨论】:

    标签: firebase listview flutter location


    【解决方案1】:

    在做fut = _gdb.getUsersInRadius(radius);的时候打电话给setState

    关于滑块部分

    FlutterSlider(
              values: [sliderValue], // initialize it with 150
              min: 0,
              max: 150,
    
          onDragging: (handlerIndex, lowerValue, upperValue) {
            radius = upperValue;
            users.clear();
            setState(){(){
                sliderValue = upperValue; // update it like this.
                fut = _gdb.getUsersInRadius(radius);
            }}
    
    
          },
        ),
    

    【讨论】:

    • 几乎可以工作,但现在我的滑块有问题,当我尝试更改值栏将开始位置并且值没有改变时
    • 如果可能的话,你能在这里提供整个屏幕的代码吗?我认为出于某种原因 FlutterSlider 正在重建
    • 滑块是转到 150 值还是 0 值?
    • 滑块每次移动都会达到 150
    • 我看到 Slider 正在重建。不要直接在 Slider 的值中使用 150,而是使用变量,例如 currentSliderValue = 150;在 onDragging 中,在 setState 方法中使用 currentSliderValue = upperValue;让我知道它是否有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2021-11-29
    • 2021-09-06
    • 1970-01-01
    • 2019-12-05
    • 2021-12-18
    相关资源
    最近更新 更多