首页 > 解决方案 > type 'some model' 不是 type 'same model' 的子类型,但它是

问题描述

当我从服务器获取数据并将其更改为名为 SetupIdeaModel 的模型时,我收到此错误。

type 'SetupIdeaModel' is not a subtype of type 'SetupIdeaModel' where
SetupIdeaModel is from package:onion/models/idea.dart
package:onion/models/Idea.dart:1
SetupIdeaModel is from package:onion/models/Idea.dart
package:onion/models/Idea.dart:1

我说 SetubIdeaModel 不是“SetupIdeaModel”类型的子类型,但它是同一个文件和同一个模型。为什么我会收到此错误。

我用这种方法获取数据。

Future getRequestDetails(String token, String requestId) async {
    Response response = await APIRequest().get(
      myUrl: "$baseUrl/innovator/idea/request/$requestId",
      token: token,
    );
    try {
      List data = [
        User().fromMap(response.data['data']['userId']),
        SetupIdeaModel().fromJson(response.data['data']['ideaId']),
        RequestModel().formJson(response.data['data']),
      ];
      return data;
    } catch (e, st) {
      print("Error $e");
      print("Stacktrace $st");
      return "Something went wrong!";
    }
  }

APIRequest() 是我为请求创建的全局方法。Ans 我也在使用 Dio 包来满足我的要求。

SetupIdeaModel

class SetupIdeaModel {
  String id;
  String userId;
  String typeIdea = "Implemented Idea";
  String category; //Or Industry
  String experienceYear;
  String experienceMonth;
  String ideaHeadline;
  String ideaText;
  Map timeline = {
    "timelineType": "date",
    "details": null,
  };
  fromJson(json) {
    return SetupIdeaModel(
      typeIdea: json['ideaType'],
      category: json['industry'],
      ideaHeadline: json['headline'],
      ideaText: json['idea'],
    );
  }

  SetupIdeaModel({
    this.uploadVideo,
    this.location,
    this.estimatedPeople,
    this.whitePaper,
    this.id,
    this.userId,
    this.typeIdea,
    this.category,
    this.experienceYear,
    this.experienceMonth,
    this.ideaHeadline,
    this.ideaText,
    this.timeline,
  });
  List documents = [];
  Map uploadVideo;
  String location;
  String estimatedPeople;
  Map whitePaper;
  bool needServiceProvider = false;
  bool needInvestor = true;
  Map toSendMap() {
    return {
      "ideaType": typeIdea,
      "industry": category,
      "industryExperienceInMonth":
          "${int.parse(experienceYear) * 12 + int.parse(experienceMonth)}",
      "headline": ideaHeadline,
      "idea": ideaText,
      "timeline": timeline,
      "uploadDocuments": documents,
      "uploadVideo": uploadVideo,
      "targetAudience": "$location",
      "uploadPaper": whitePaper,
      "estimatedPeople": estimatedPeople,
      "needServiceProvider": "$needServiceProvider",
      "needInvestor": "$needInvestor",
    };
  }
}

将模型与 is FutureBuilder 一起使用

FutureBuilder(
        future: getData,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            User _user = snapshot.data[0];
            SetupIdeaModel _idea = snapshot.data[1];
            RequestModel _request = snapshot.data[2];
            return SingleChildScrollView(
              child: Padding(
                padding: const EdgeInsets.all(10),
                child: Column(
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "${_idea.typeIdea}",
                          style: TextStyle(
                            color: deepBlue,
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        IconButton(
                          icon: Icon(Icons.info_outline,
                              color: middlePurple, size: 20),
                          onPressed: () {},
                        ),
                      ],
                    ),
                    Card(
                      elevation: 4,
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 10, vertical: 10),
                        child: Column(children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                "${_request.postedOn}",
                                style: TextStyle(color: Colors.black),
                              ),
                            ],
                          ),
                          SizedBox(height: 10),
                          Container(
                            padding: EdgeInsets.all(10),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(5),
                              border: Border.all(width: 0.5, color: deepGrey),
                            ),
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text("Industry",
                                        style: TextStyle(color: deepGrey)),
                                    Text(
                                      "${_idea.category}",
                                      style: TextStyle(color: Colors.black),
                                    ),
                                  ],
                                ),
                                Divider(),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text("Idea Headline",
                                        style: TextStyle(color: deepGrey)),
                                    Text(
                                      "${_idea.ideaHeadline}",
                                      style: TextStyle(color: Colors.black),
                                    ),
                                  ],
                                ),
                                Divider(),
                                SizedBox(height: 5),
                                Align(
                                  alignment: Alignment.centerLeft,
                                  child: RichText(
                                    text: TextSpan(
                                      text: 'Idea: ',
                                      style: TextStyle(color: Colors.black),
                                      children: <TextSpan>[
                                        TextSpan(
                                          style: TextStyle(
                                              color: deepGrey,
                                              fontWeight: FontWeight.normal),
                                          text: "${_idea.ideaText}",
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                          SizedBox(
                            height: 10,
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Row(children: [
                                      CircleAvatar(
                                        backgroundImage: NetworkImage(
                                            "${_user.profile ?? 'https://i.pravatar.cc/300'}"),
                                      ),
                                      SizedBox(width: 5),
                                      Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text("${_user.name}",
                                              style: TextStyle(
                                                  fontWeight: FontWeight.bold)),
                                          Text("${_user.industry}",
                                              style:
                                                  TextStyle(color: deepGrey)),
                                          Row(children: [
                                            Text("Rated: ",
                                                style:
                                                    TextStyle(color: deepGrey)),
                                            MyFiveRating(rateVal: _user.rate),
                                          ])
                                        ],
                                      ),
                                    ]),
                                    Row(children: [
                                      InkWell(
                                        child: Icon(Icons.more_vert,
                                            color: deepGrey),
                                        onTap: () {},
                                      ),
                                    ])
                                  ],
                                ),
                                Divider(),
                                Row(children: [
                                  Text("Interested in: "),
                                  Icon(Icons.radio_button_checked,
                                      color: middlePurple),
                                  Text("${_request.investAs.type}"),
                                ]),
                                Divider(),
                                RichText(
                                  text: TextSpan(
                                    text: 'Message: ',
                                    style: TextStyle(color: Colors.black),
                                    children: <TextSpan>[
                                      TextSpan(
                                          style: TextStyle(
                                            color: deepGrey,
                                            fontWeight: FontWeight.normal,
                                          ),
                                          text:
                                              "This is my message to you. Yes you, Who are reading this text.I'm with you how are you I'm fine thanks. Everything is goin well and what about you bro."),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          )
                        ]),
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Column(
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              GlowCheckbox(
                                color: Theme.of(context).primaryColor,
                                value: checkboxSelected,
                                enable: true,
                                onChange: (bool value) {
                                  setState(() {
                                    checkboxSelected = !checkboxSelected;
                                  });
                                },
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Expanded(
                                // child: Text(
                                //   "By Cheacking the box you agree to out terms and services",
                                // ),
                                child: RichText(
                                  text: TextSpan(
                                    text: 'Agree to ',
                                    style: TextStyle(color: Colors.black),
                                    children: <TextSpan>[
                                      TextSpan(
                                        text: 'Term and Condition',
                                        style: TextStyle(
                                            color: middlePurple,
                                            decoration:
                                                TextDecoration.underline),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            showDialog(
                                              context: context,
                                              builder: (context) {
                                                return TandCDialog();
                                              },
                                            );
                                          },
                                      ),
                                      TextSpan(
                                          text:
                                              ' and contract while Requesting this Franchise!'),
                                    ],
                                  ),
                                ),
                              )
                            ],
                          ),
                          SizedBox(height: 5),
                          SizedBox(
                            width: double.infinity,
                            child: RaisedButton(
                              child: Text(
                                "Accept Add On Project",
                                style: TextStyle(
                                  fontSize: 18,
                                  color: Colors.white,
                                ),
                              ),
                              color: middlePurple,
                              onPressed: () {
                                acceptAddOnProject(true, _scaffoldKey);
                              },
                            ),
                          ),
                          SizedBox(
                            width: double.infinity,
                            child: OutlineButton(
                              child: Text(
                                "Declient",
                                style: TextStyle(
                                  fontSize: 18,
                                ),
                              ),
                              onPressed: () {
                                acceptAddOnProject(false, _scaffoldKey);
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            );
          } else if (snapshot.hasError) {
            print("error ${snapshot.error}");
            return Center(
                child: Text(
                    "Something went wrong, While getting data from the server."));
          } else {
            return Center(child: SingleChildScrollView());
          }
        },
      ),

标签: flutterdarttypes

解决方案


Flutter 不会将这两条路径识别为相同的,因为它们的大小写不同。修复您的导入,以便它们引用相同的文件。


推荐阅读