我正在尝试在1个文件中制作所有字符串,但是当我尝试它时会产生一个错误Only static members can be accessed in initializers
我想将所有字符串放在一个文件中,并且可以使用所有类的getter方法调用它,无论是普通类,具有StatefullWidget的类还是具有StatelessWidget的类

错误Only static members can be accessed in initializers导致ApiService类中的configStrings

class ConfigStrings {
  String _blogUrl = "https://blogspot-to-app.blogspot.com/";

  String get blogUrl => _blogUrl;
}


import 'package:blogspot/config/config_strings.dart';
import 'package:http/http.dart';

class ApiServices {
  var configStrings = ConfigStrings();
  final String baseUrl = configStrings.blogUrl;
  Client client = Client();
}

最佳答案

您需要在方法或构造函数中访问configStrings:

import 'package:blogspot/config/config_strings.dart';
import 'package:http/http.dart';

class ApiServices {
  var configStrings = ConfigStrings();
  ApiServices(){
      final String baseUrl = configStrings.blogUrl;
  }
 Client client = Client();
}

关于flutter - 当不同的文件时,getter发生抖动错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62357814/

10-09 04:20