skip to Main Content

I get this error in Flutter i’m trying to use data but the constructor did’t allow and gives two error
1/ error: ‘Category’ isn’t a function. (invocation_of_non_function at [torrism] libapp_data.dart:8)
2/error: The name ‘Category’ is defined in the libraries ‘package:flutter/src/foundation/annotations.dart (via package:flutter/foundation.dart)’ and ‘package:torrism/models/categorie.dart’. (ambiguous_import at [torrism] libapp_data.dart:8)

this the code:

import 'package:flutter/foundation.dart'; 
import'./models/categorie.dart';
 
 List Categories_data=  [ 
Category(   
   id: 'c1',
   title: 'جبال',  
   imageUrl:  
 'https://images.unsplash.com/photo-1575728252059-db43d03fc2de?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NTh8fG1vdW5hdGluc3xlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=400&q=',
 ), ];

and this the class and the constructor :

import 'package:flutter/material.dart';

class Category {
  final String id;
  final String title;
  final String imageUrl;

 Category( {required this.id, required this.title, required this.imageUrl});

}

i try everthing nothing happend

2

Answers


  1.  List<Category> categories = [Category(id: "id", title: 'title', imageUrl: 'imgUrl')];
    

    or

      List<Category> categories = [];
    
      final category = Category(id: "id", title: 'title', imageUrl: 'imgUrl');
    
      categories.add(category);
    
    Login or Signup to reply.
  2. It’s because Category class is defined on both class you have import

    import 'package:flutter/foundation.dart'; 
    import'./models/categorie.dart';
    

    so the complier dont know which one to use. To fix it, you must hide one Category class like this:

    import 'package:flutter/foundation.dart' hide Category;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search