无法在初始化程序中访问实例成员 'totalfood'。尝试用不同的表达式替换对实例成员的引用

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

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},
];

给出错误实例成员 'totalfood' 无法在初始化程序中访问。 尝试用不同的表达式替换对实例成员的引用。

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);
}

这是图片

category 是静态的,在这种情况下 totalfood 必须是静态的。

你可以这样做

class AppColors {
  static late final double totalfood;

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

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