Home > Software engineering >  The instance member 'totalfood' can't be accessed in an initializer. Try replacing th
The instance member 'totalfood' can't be accessed in an initializer. Try replacing th

Time:02-17

The `TotalFood` in the 
static List category = [
  {"name": "Food", "amount": totalfood},
  {"name": "Entertainment", "amount": 100.0},
  {"name": "Personal", "amount": 80.0},
  {"name": "Transportation", "amount": 50.0},
  {"name": "Studies", "amount": 100.0},
  {"name": "Any", "amount": 30.0},
];

gives error The instance member 'totalfood' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression.

import 'package:flutter/material.dart';

class AppColors {
  final double totalfood;
  const AppColors(this.totalfood);
  static List pieColors = [
    Colors.indigo[400],
    Colors.blue,
    Colors.green,
    Colors.amber,
    Colors.deepOrange,
    Colors.brown,
  ];

  static List category = [
    {"name": "Food", "amount": totalfood},
    {"name": "Entertainment", "amount": 100.0},
    {"name": "Personal", "amount": 80.0},
    {"name": "Transportation", "amount": 50.0},
    {"name": "Studies", "amount": 100.0},
    {"name": "Any", "amount": 30.0},
  ];

  static Color primaryWhite = Color(0xffedf1f4);
}

Here’s the image

CodePudding user response:

The category is static and totalfood have to be static in this case.

You can do like this

class AppColors {
  static late final double totalfood;

  void _totalFood(double tf) {
    totalfood = tf;
  }

  AppColors({required double totalFood}) {
    _totalFood(totalFood);
  }
//....
  • Related