首页 > 解决方案 > 错误:没有为类“对象?”定义方法“数据”。- “对象”来自“飞镖:核心”

问题描述

错误:

警告:null-aware 操作的操作数 '??' 具有不包括空值的“字符串”类型。"$${document["price"]}" ?? “价格”,^ lib/screens/product_page.dart:38:65:错误:方法“数据”没有为“对象?”类定义。

FAILURE:构建失败并出现异常。

Process 'command 'C:\src\flutter\bin\flutter.bat'' 以非零退出值 1 结束

BUILD FAILED in 36s Exception: Gradle task assembleDebug failed with exit code 1

代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:your_store/constants.dart';
import 'package:your_store/widgets/custom_action_bar.dart';
import 'package:your_store/widgets/image_swipe.dart';

class ProductPage extends StatefulWidget {
  final String? productId;
  ProductPage({this.productId});

  @override
  _ProductPageState createState() => _ProductPageState();
}

class _ProductPageState extends State<ProductPage> {
  final CollectionReference _productRef =
      FirebaseFirestore.instance.collection("Products");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
      children: [
        FutureBuilder(
          future: _productRef.doc(widget.productId).get(),
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Scaffold(
                body: Center(
                  child: Text("Error: ${snapshot.error}"),
                ),
              );
            }

            if (snapshot.connectionState == ConnectionState.done) {
              //Firebase document data map
              Map<String, dynamic> documentData = snapshot.data.data();

              //List of images
              List imageList = documentData["images"];

              return ListView(
                padding: EdgeInsets.all(0),
                children: [
                  ImageSwipe(imageList: imageList,),
                  Padding(
                    padding: const EdgeInsets.only(
                      top: 24.0,
                      left: 24.0,
                      right: 24.0,
                      bottom: 4.0,
                    ),
                    child: Text(
                      "\$${documentData["name"]}",
                      style: Constants.boldHeading,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: 4.0,
                      horizontal: 24.0,
                    ),
                    child: Text(
                      "\$${documentData["price"]}",
                      style: TextStyle(
                        fontSize: 18.0,
                        color: Theme.of(context).accentColor,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: 8.0,
                      horizontal: 24.0,
                    ),
                    child: Text(
                      "\$${documentData["desc"]}",
                      style: TextStyle(fontSize: 16.0),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      vertical: 24.0,
                      horizontal: 24.0,
                    ),
                    child: Text(
                      "Selected Size",
                      style: Constants.regularDarkText,
                    ),
                  ),
                ],
              );
            }

            //Loading State
            return Scaffold(
              body: Center(
                child: CircularProgressIndicator(),
              ),
            );
          },
        ),
        CustomActionBar(
          hasBackArrow: true,
          hasTitle: false,
          hasBackground: false,
        ),
      ],
    ));
  }
}

标签: androidflutterdartflutter-layoutflutter-dependencies

解决方案


我认为您的第二个比较条件不完整,而不是第二个,如果请使用 if-else,此条件。

else if(snapshot.connectionState == ConnectionState.done && snapshot.hasData){...}

推荐阅读