I was trying to send data got from a JSON response to a second screen from the main screen.
My widgets are placed in separate class files. I called a single API call to fetch all data from the API so that there won't be many API calls happening while opening the app.

But I can't pass the data to the second screen in arrMaincategories. What should I do?

class HomeScreen extends StatefulWidget {
      _HomeScreenState createState() => _HomeScreenState();

    class _HomeScreenState extends State<HomeScreen> {
    var arrBanner = [];
      var arrImages = [];
      var arrMainCategories = [];
      void initState() {

        setState(() {});

      getBanner() async {
        String url = "https://www.clickxpress.in/api/";

        var prefs = await SharedPreferences.getInstance();

        var custID = prefs.getString('id');

        var response = await http.post(Uri.encodeFull(url),
            body: (<String, String>{
              'type': "homepage",
              'secKey': "2a067feaeb67fgh89fyrc6657654fhjj9714b2094818f",
              'customerId': custID,
              'geoLat': "55.2324",
              'geoLong': "23.55556",
            })); //for accepting only json response
        setState(() {
          Map<String, dynamic> map = json.decode(response.body);
          var data = map["attributes"];
    arrBanner = data['topBanner'];
          arrMainCategories = data['mainCategories'];
    if (arrBanner != null) {
            for (var value in arrBanner) {
              final image = value['imageUrl'];

Called It to a new screen

NewCat(arrMainCategories: arrMainCategories),

trying to fetch on 2nd screen

class NewCat extends StatefulWidget {
  NewCat({Key key, @required this.arrMainCategories}) : super(key: key);
  final arrMainCategories;

  _NewCatState createState() => _NewCatState(arrMainCategories);

class _NewCatState extends State<NewCat> {
  var arrMainCategories = [];
  var arrCatImages = [];
  var arrCatName = [];


But the value is not coming here in the empty array. what to do?

Solution 1: Mukul

Instead of using the Constructor, You Can also access the data by using this line of code


See this resource link for more details

Solution 2: Hakkı Akut

use this


instead of this