Flutter 2.0 The return type of getter 'xxxx' is 'dynamic' which isn't a subtype of the type 'List<dynamic>' of its setter 'xxxx'

Issue

I’m recently migrate my code to flutter 2.0, but I’m getting this error:
error: The return type of getter ‘tabbarcatinfo’ is ‘dynamic’ which isn’t a subtype of the type ‘List’ of its setter ‘tabbarcatinfo’.

import 'package:flutter/material.dart';
List _tabbarcatinfo = [];

class TabBarCategoriesInfo with  ChangeNotifier{
  static late List<String> name;

  get tabbarcatinfo {
    return _tabbarcatinfo;
  }

  set tabbarcatinfo(List Listita) {
    _tabbarcatinfo = Listita;
    notifyListeners();
  }

  void addData(List Listita) {
    _tabbarcatinfo.add(Listita);
    //notifyListeners();
  }
}

Solution

You have not properly defined types in your code.

Use it like this.

List<String> _tabbarcatinfo = [];

class TabBarCategoriesInfo with ChangeNotifier {
  static late List<String> name;

  List<String> get tabbarcatinfo {
    return _tabbarcatinfo;
  }

  set tabbarcatinfo(List<String> Listita) {
    _tabbarcatinfo = Listita;
    notifyListeners();
  }

  void addData(String item) {
    _tabbarcatinfo.add(item);
    //notifyListeners();
  }
}

Comment if you have any particular doubt about a line and I will elaborate by answer to explain it.

Answered By – Nisanth Reddy

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *