我正在尝试将所有字符串都放在一个文件中,但是当我尝试它时,会产生错误Only static members can be accessed in initializers
我想将所有字符串放在一个文件中,并且可以使用所有类的getter方法调用它,无论是普通类,具有StatefullWidget的类还是具有StatelessWidget的类
在ApiService类中导致错误Only static members can be accessed in initializers
的错误configStrings
我的config_string.dart中的代码
class ConfigStrings {
String _blogUrl = "https://blogspot-to-app.blogspot.com/";
String get blogUrl => _blogUrl;
}
我在api_service.dart中的代码
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();
}
答案 0 :(得分:1)
您需要在方法或构造函数内部访问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();
}