Flutter中如何定义PostData类的多个构造器避免‘默认构造器已定义’错误?
解决Dart中“默认构造器已定义”的错误
嗨,我看到你在Flutter里创建PostData类时遇到了构造器冲突的问题,这是因为Dart不支持构造函数重载——也就是说你不能定义两个没有名字的默认构造器。不过不用删太多代码,我们可以用Dart的命名构造器来解决这个问题,同时保留你需要的所有功能。
第一步:重命名冲突的构造器
你现在有两个默认构造器(一个是命名参数的,一个是位置参数的),我们把位置参数的那个改成命名构造器,比如叫PostData.fromValues:
// 保留原来的命名参数默认构造器 PostData({ this.likesCount, this.likes, this.timestamp, this.postersName, this.postersId, this.postersImageUrl, this.postSavedByUsers, this.title, this.id, this.description, this.price, this.imageUrl, this.productCategoryName, this.brand, this.isPopular, }); // 把位置参数的构造器改成命名构造器 PostData.fromValues( int likesCount, List likes, int timestamp, String postersName, String postersId, String postersImageUrl, List postSavedByUsers, String title, // 补上你之前遗漏的String类型声明 String id, String description, double price, String imageUrl, String productCategoryName, String brand, bool isPopular, ) { this.likesCount = likesCount; this.likes = likes; this.timestamp = timestamp; this.postersName = postersName; this.postersId = postersId; this.postersImageUrl = postersImageUrl; this.postSavedByUsers = postSavedByUsers; this.title = title; this.id = id; this.description = description; this.price = price; this.imageUrl = imageUrl; this.productCategoryName = productCategoryName; this.brand = brand; this.isPopular = isPopular; }
第二步:更新调用该构造器的方法
你的mapToObject方法里原来调用的是位置参数构造器,现在要改成调用新的命名构造器:
PostData mapToObject(Map<String, dynamic> map) { return PostData.fromValues( map["likes_count"], map["likes"], map["timestamp"], map["posters_name"], map["posters_id"], map["posters_image_url"], map["post_saved"], map["title"], map["id"], map["description"], map["price"], map["imageUrl"], map["productCategoryName"], map["brand"], map["isPopular"], ); }
第三步:完善PostData.zero()构造器
你定义的PostData.zero()目前是空的,我们可以让它初始化所有字段为合理的默认值,避免后续空指针问题:
PostData.zero() : likesCount = 0, likes = [], timestamp = 0, postersName = "", postersId = "", postersImageUrl = "", postSavedByUsers = [], title = "", id = "", description = "", price = 0.0, imageUrl = "", productCategoryName = "", brand = "", isPopular = false;
完整修改后的代码
整合所有修改后,完整的PostData类代码如下:
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/cupertino.dart'; class PostData with ChangeNotifier{ int likesCount; List likes; int timestamp; String postersName; String postersId; String postersImageUrl; List postSavedByUsers; String title; String id; String description; double price; String imageUrl; String productCategoryName; String brand; bool isPopular; // 默认命名参数构造器 PostData({ this.likesCount, this.likes, this.timestamp, this.postersName, this.postersId, this.postersImageUrl, this.postSavedByUsers, this.title, this.id, this.description, this.price, this.imageUrl, this.productCategoryName, this.brand, this.isPopular, }); // 命名构造器:从位置参数创建对象 PostData.fromValues( int likesCount, List likes, int timestamp, String postersName, String postersId, String postersImageUrl, List postSavedByUsers, String title, String id, String description, double price, String imageUrl, String productCategoryName, String brand, bool isPopular, ) { this.likesCount = likesCount; this.likes = likes; this.timestamp = timestamp; this.postersName = postersName; this.postersId = postersId; this.postersImageUrl = postersImageUrl; this.postSavedByUsers = postSavedByUsers; this.title = title; this.id = id; this.description = description; this.price = price; this.imageUrl = imageUrl; this.productCategoryName = productCategoryName; this.brand = brand; this.isPopular = isPopular; } // 命名构造器:创建默认值对象 PostData.zero() : likesCount = 0, likes = [], timestamp = 0, postersName = "", postersId = "", postersImageUrl = "", postSavedByUsers = [], title = "", id = "", description = "", price = 0.0, imageUrl = "", productCategoryName = "", brand = "", isPopular = false; PostData mapToObject(Map<String, dynamic> map) { return PostData.fromValues( map["likes_count"], map["likes"], map["timestamp"], map["posters_name"], map["posters_id"], map["posters_image_url"], map["post_saved"], map["title"], map["id"], map["description"], map["price"], map["imageUrl"], map["productCategoryName"], map["brand"], map["isPopular"], ); } List<PostData> forArrayToObject( List<QueryDocumentSnapshot<Map<String, dynamic>>> data) { List<PostData> postListData = []; for (var object in data) { postListData.add(mapToObject(object.data())); } return postListData; } Map<String, dynamic> toMap() { Map<String, dynamic> map = { "likes_count": this.likesCount, "likes": this.likes, "posters_name": this.postersName, "posters_id": this.postersId, "posters_image_url": this.postersImageUrl, "post_saved": this.postSavedByUsers, "title": this.title, "id": this.id, "timestamp": this.timestamp, "description": this.description, "price": this.price, "imageUrl": this.imageUrl, "productCategoryName": this.productCategoryName, "brand": this.brand, "isPopular": this.isPopular, }; return map; } }
为什么这样可行?
Dart允许你定义多个命名构造器,每个构造器有唯一的名字,这样就不会和默认构造器冲突了。命名构造器的格式是ClassName.constructorName(),非常适合区分不同的对象创建方式,完全符合你“无需大量删除代码”的需求。
内容的提问来源于stack exchange,提问作者Dijon
相关产品推荐
相关产品推荐

