Getter on flutter error when different files - flutter

I'm trying to make all strings in 1 file, but when I try it it produces an error Only static members can be accessed in initializers
I want to put all strings in 1 file and can be called with the getter method for all classes, be it an ordinary class, a class with StatefullWidget, and a class with StatelessWidget
error Only static members can be accessed in initializers leading to configStrings in the ApiService class
My Code in config_string.dart
class ConfigStrings {
String _blogUrl = "https://blogspot-to-app.blogspot.com/";
String get blogUrl => _blogUrl;
}
My Code in 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();
}

You need to access configStrings inside a method or the constructor:
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();
}

Related

Hive: generate adapters for classes in multiple .dart files

I have classes defined in individual files. I want to store all of the classes in a single Hive box. I cannot see how to register the adapters for the separate files and have them generated into a single .part file. Do I need to create a method and explicitly call the method to register the adapters? I've tried registering them in the Main() method of main file, but that doesn't work. After running build_runner, the additional adapters are not in the .part file.
CLASS A
import 'package:hive/hive.dart';
import 'package:my_project/class_b.dart';
import 'package:my_project/class_c.dart';
...
#HiveType(typeId: 0)
class ClassA {
#HiveField(0)
String string ="";
...
void main(List<String> arguments) async {
var path = "assets/";
Hive
..registerAdapter(ClassAAdapter());
..registerAdapter(ClassBAdapter()); //???
...
}
CLASS B
import 'package:hive/hive.dart';
...
#HiveType(typeId: 1)
class ClassB {
#HiveField(0)
String string ="";
...
void initAdapters() { //???
var path = "assets/";
Hive // ??????
..registerAdapter(ClassBAdapter()); //???
}

How to call javascript object methods from Dart/Flutter using package:js?

I'm using Flutter web. I have a javascript class with methods. I can call the constructor from Dart with package:js, and I can access members with getters, but when I try to call a class method I get a NoSuchMethodError. How do I make the class methods available to Dart?
In my index.html I have this in a script block:
class TestClass {
constructor(val) {
this.foo = val;
console.log("TestClass constructed with foo="+this.foo);
}
someGenericFunction() {
console.log("generic");
}
getSomeValue(foo, bar) {
return this.foo+" "+foo+" "+bar;
}
}
This is my Dart interop code:
#JS()
library testclass.js;
import 'package:js/js.dart';
#anonymous
#JS()
abstract class TestClass {
external factory TestClass({String foo});
external String get foo;
external void someGenericFunction();
external String getSomeValue(String foo, String bar);
}
Here's my Dart code:
TestClass ts = TestClass(foo: "dart"); //Works!
String foo = ts.foo; //Works!
//The below methods error with:
//JSNoSuchMethodError (NoSuchMethodError: tried to call a non-function...)
ts.someGenericFunction();
ts.someGenericFunction.call();
String testClassString = ts.getSomeValue("one", "two");
The constructor works and the getter works. How do I correctly expose the TestClass methods?

Import a list of string from another file in flutter

I would create a class with a list of string in flutter on a another file and import this file to a main file that I am working.... for example:
class OrderDetails {
final email = 'Email';
final name = 'Mario';
final city = 'Milano';
}
have this class into a file called details.dart
and import these strings to another main file
details.dart
class OrderDetails {
static final email = 'Email';
static final name = 'Mario';
static final city = 'Milano';
}
import this file to that file where you want to use this file using
import '/your_file_path/details.dart';
now use those value like this
OrderDetails.email etc

Dart: Referring to Singleton class from within itself

I have a singleton class and I would like to refer to it from within itself, however I am getting a CyclicInitializationError (Reading static variable 'instance' during its initialization) when trying to do so.
Here is an example:
void main(){
Singleton newSingleton = Singleton();
print(newSingleton.member);
}
class Singleton {
static final instance = Singleton._internal();
Singleton._internal();
factory Singleton(){
return instance;
}
String member = "hello";
String hamburger = instance.member; // This line causes the error to be thrown
}

Instance field access outside constructor

I am programming a flutter app and have written dart class.
When I call the classes constructor, I the following error at runtime...
error: Instance field access outside constructor
The class looks as follows...
import '../framework/server.dart';
import 'package:meta/meta.dart';
class GamesRequestMessage extends Message {
String getName() => "GamesRequestMessage";
String idToken;
GamesRequestMessage({ #required this.idToken = idToken });
JsonObject content()
{
JsonObject content = JsonObject();
content.writeString("idToken" , idToken);
}
}
And the calling code where the error occurs is as follows...
var msg = GamesRequestMessage (idToken : idToken.token);
What has gone wrong? Also, why is this error not a compilation time error?
this.idToken = idToken should be just this.idToken