Home > Back-end >  How to fix Flutter error: "Type 'Item' not found."
How to fix Flutter error: "Type 'Item' not found."

Time:09-21

This code has been working for several weeks, but suddenly brought up errors after an AndroidStudio update:

    class _GameState extends State<Game> {
  List<Item> its = [
    Item('fins','images/idea-fins.png'),
    Item('sonar','images/idea-sonar.png'),
    Item('suction','images/idea-suction.png'),
    Item('tires','images/idea-tire.png'),
    Item('velcro','images/idea-velcro.png'),
    Item('propeller','images/idea-propeller.png')
  ];

The error I get is:

    Error: Type 'Item' not found.
  List<Item> its = [

Does "Item" now have to be defined, somehow, or has some more fundamental change been made?

CodePudding user response:

1st case : You havent import the package please import the package

example:

import 'package:flutter/material.dart';

2nd Case : You have imported it already but still showing this . then do like this :

example:

import 'package:flutter/material.dart' as app; **//in your case as Items for your package url** 

then do like this:

   class _GameState extends State<Game> {
  List<Items.Item> its = [
    Items .Item('fins','images/idea-fins.png'),
    Items.Item('sonar','images/idea-sonar.png'),
   Items.Item('suction','images/idea-suction.png'),
   Items.Item('tires','images/idea-tire.png'),
    Items.Item('velcro','images/idea-velcro.png'),
    Items.Item('propeller','images/idea-propeller.png')
  ];

It cause because of same package name declared

CodePudding user response:

When I

import 'package:flutter/material.dart' as Items;

and do this:

        class _GameState extends State<Game> {
  List<Items.Item> its = [
    Items.Item('fins','images/idea-fins.png'),
    Items.Item('sonar','images/idea-sonar.png'),
    Items.Item('suction','images/idea-suction.png'),
    Items.Item('tires','images/idea-tire.png'),
    Items.Item('velcro','images/idea-velcro.png'),
    Items.Item('propeller','images/idea-propeller.png')
  ];

I get:

Unhandled exception:
Crash when compiling null,
at character offset null:
Null check operator used on a null value
#0      InferableTypeBuilderMixin.type (package:front_end/src/fasta/builder/type_builder.dart:392:29)
#1      InferableTypeBuilder.inferType (package:front_end/src/fasta/builder/omitted_type_builder.dart:155:12)

And a whole lot more!

  • Related