【问题标题】:Pass snapshot data to Flutter Widget将快照数据传递给 Flutter Widget
【发布时间】:2020-12-17 23:22:19
【问题描述】:

我正在尝试将数据从快照传递到小部件,以便渲染数据,但出现以下错误

lib/screens/RetailerList.dart:215:35: Error: Too few positional arguments: 1 required, 0 given.
                  ? _buildRetailer(retailer: snapshot.data)

小部件

Widget _buildRetailer(Retailer retailer) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(
          MaterialPageRoute(
              // builder: (_) => RetailerDetails(retailer: retailer[0]),
              ),
        );
      },
      child: Padding(
        padding: EdgeInsets.only(left: 40.0, bottom: 30.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Hero(
              tag: retailer.slug,
              child: Container(
                width: double.infinity,
                height: 250.0,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.0),
                    bottomLeft: Radius.circular(20.0),
                  ),
                  image: DecorationImage(
                    image: NetworkImage(
                      "https://site-assets.afterpay.com/assets/favicon/apple-touch-icon-47062a004c5b1440ea8159b43580154540d76df61dc55dc87522378f8f76bbec.png",
                    ),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(12.0, 12.0, 40.0, 0.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    retailer.name,
                    style: TextStyle(
                      fontFamily: 'Montserrat',
                      fontSize: 24.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  IconButton(
                    icon: Icon(Icons.favorite_border),
                    iconSize: 30.0,
                    color: Color(0xFFFD6456),
                    onPressed: () => print('Favorite'),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(12.0, 0.0, 40.0, 12.0),
              child: Text(
                retailer.shortDescription,
                style: TextStyle(
                  fontFamily: 'Montserrat',
                  fontSize: 16.0,
                  color: Colors.grey,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Theme.of(context).platform == TargetPlatform.iOS
          ? CupertinoNavigationBar(
              actionsForegroundColor: Color.fromRGBO(108, 212, 196, 1),
              middle: Text('Explore Retailers',
                  style: GoogleFonts.nunito(
                      fontWeight: FontWeight.w700,
                      textStyle: TextStyle(
                          color: Color.fromRGBO(98, 49, 158, 1),
                          letterSpacing: .5,
                          fontSize: 20))),
              leading: IconButton(
                tooltip: 'Filter Retailers',
                icon: Icon(IconData(0xF4A6,
                    fontFamily: CupertinoIcons.iconFont,
                    fontPackage: CupertinoIcons.iconFontPackage)),
                // onPressed: _onButtonPressed
              ),
            )
          : AppBar(
              title: Text('Explore Retailers',
                  style: GoogleFonts.nunito(fontWeight: FontWeight.w700)),
              actions: <Widget>[
                IconButton(
                  tooltip: 'Filter Retailers',
                  icon: Icon(Icons.filter_list),
                  // onPressed: _onButtonPressed
                )
              ],
            ),
      backgroundColor: Colors.white,
      body: ListView(
        children: <Widget>[
          SizedBox(height: 10.0),
          Container(height: 100.0, child: _buildListView()),
          SizedBox(height: 50.0),
          StreamBuilder<List<Retailer>>(
            stream: fetchRetailers().asStream(),
            builder: (context, snapshot) {
              if (snapshot.hasError) print(snapshot.error);
              return snapshot.hasData
                  ? _buildRetailer(retailer: snapshot.data)
                  : Center(child: CircularProgressIndicator());
            },
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter snapshot flutter-widget


    【解决方案1】:

    而不是buildRetailer(retailer: snapshot.data)

    buildRetailer(snapshot.data)

    由于您将数据传递给不存在的命名参数retailer,因为您在buildRetailer 方法中定义了positional parameter

    【讨论】:

    • 有点用,但我现在没有收到新错误The following NoSuchMethodError was thrown building StreamBuilder&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt;(dirty, state: _StreamBuilderBaseState&lt;List&lt;Map&lt;String, dynamic&gt;&gt;, AsyncSnapshot&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt;&gt;#a9c41): Class 'List&lt;Map&lt;String, dynamic&gt;&gt;' has no instance getter 'slug'. Receiver: Instance(length:10) of '_GrowableList'
    猜你喜欢
    • 2020-02-27
    • 2020-01-21
    • 2020-03-01
    • 2020-06-06
    • 2021-04-24
    • 2020-02-02
    • 2021-05-28
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多