How can I define PostData twice

Viewed 33

I am creating a class PostData to store my info from firebase. When I try to define PostData twice, it gives me an error. Is there any way I can define postdata twice without getting an error or is there any way I can have no errors in the code without deleting much. In my console, the error says, error: The default constructor is already defined. I am new to flutter so I don't really know how to solve this error. If you need any other information, please make a comment and I'll reply. I also can explain more if something doesn't make sense. Here is my full PostData class

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(
    
          int likesCount,
          List likes,
          int timestamp,
    
          String postersName,
          String postersId,
          String postersImageUrl,
          List postSavedByUsers,
            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();
    
      PostData mapToObject(Map<String, dynamic> map) {
        return PostData(
    
            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;
      }
    
    }
1 Answers

I encourage you to spend some time working through tutorials to gain a better understanding of Flutter|Dart.

It's great to dive in and write code but you'll hinder yourself without developing a foundational understanding of the language before you continue. Any time you spend now learning the language, will pay dividends.

In this case, you're defining a class called PostData, with a set of instance variables (e.g. likesCount) and a constructor (function) using the same name as the class, i.e. also PostData.

As you've seen, you cannot duplicate a function name in Dart; you cannot have more than one function with the same name. This is called overloading but is not permitted in Dart.

Here's a good overview of Dart classes and constructors

Related