【问题标题】:How to access index of Future<List<T>> data in another page Flutter如何在另一个页面 Flutter 中访问 Future<List<T>> 数据的索引
【发布时间】:2022-01-11 17:14:17
【问题描述】:

我在一个页面上从第三方 API 获取数据列表并在另一个页面上使用相同的数据,但我不知道如何使列表可迭代以访问 Widget Build() 方法中的索引。

在此页面上,我正在获取列表。

class DetailsPage extends StatefulWidget {
  List<MarkersOnMap> propertydetails;
  DetailsPage({Key key, this.propertydetails})
      : super(
          key: key,
        );
  @override
  _DetailsPageState createState() => _DetailsPageState();
}

这是一个 Build() 方法,我想在每次调用时将索引作为变量访问,目前我只是使用索引 [0]

Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.propertydetails[0].ePropertiesCardsList.name,
            style: appBarTextStyle),
        
      ),
      bottomNavigationBar: Material(
        child: Container(
          child: Row(
            children: <Widget>[
              Row(
                children: [
                  Text(
                    widget.propertydetails[0].ePropertiesCardsList.price
                        .toString(),
                    style: blackBigBoldTextStyle,
                  ),
                  Text(
                    widget.propertydetails[0].ePropertiesCardsList.status,
                    style: blackSmallTextStyle,
                  ),
                ],
              ),
                child: Container(
                  child: Text(
                    'Contact'),
                ),
              ),
            ],
          ),
        ),
      ),
      body: ListView(
        children: [
          Hero(
            tag: widget.propertydetails[0].ePropertiesCardsList.name,
            child: slider(),
          ),
          titleRating(),
        ],
      ),
    );
  }

从页面发送数据,如下所示

_propertyFinalList(index) {
    return Container(
      child: Center(
        child: InkWell(
          onTap: () async {
            var get = await future;
            if (get.isNotEmpty) {
              Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => DetailsPage(
                          propertydetails: get,
                        )),
              );
            }
          },

【问题讨论】:

    标签: flutter indexing send


    【解决方案1】:

    不是 100% 确定我的问题是正确的,但为了简单起见,您可以使用全局静态类来存储数据并从应用程序的不同部分访问它。

    class GlobalState {
      static List<MarkersOnMap> propertydetails = [];
      static int selectedIndex = 0;
    }
    

    然后您将能够从不同的页面写入和读取这些数据。请记住,如果您更改此 GlobalState 中的数据,您的视图将不会重新呈现,您可以查看:https://pub.dev/packages/get

    【讨论】:

    • 感谢您的回复。实际上我在地图上使用googlemaps和标记的详细信息卡,所以我已经从这个页面上的api获取数据但是当我点击卡片查看特定卡片/标记的详细信息页面时,这是我被卡住的部分,我无法将 index 称为变量,例如 widget.propertydetails[index].eProper......“widget.propertydetails[0].ePropertiesCardsList.name”
    【解决方案2】:

    我刚刚通过更改以下代码解决了这个问题

    MaterialPageRoute(
                    builder: (context) => DetailsPage(
                          propertydetails: get,
                        )),
              );
    

    到这个代码。完整的代码可以在上面的问题中找到。

    MaterialPageRoute(
                    builder: (context) => DetailsPage(
                          propertydetails: [get[index]],
                        )),
              );
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      相关资源
      最近更新 更多