Enum from String

Mirrors aren’t always available, but fortunately you don’t need them. This is reasonably compact and should do what you want. enum Fruit { apple, banana } // Convert to string String str = Fruit.banana.toString(); // Convert to enum Fruit f = Fruit.values.firstWhere((e) => e.toString() == ‘Fruit.’ + str); assert(f == Fruit.banana); // it worked Thanks … Read more

Converting timestamp

Your timestamp format is in fact in Seconds (Unix timestamp) as opposed to microseconds. If so the answer is as follows: Change: var date = new DateTime.fromMicrosecondsSinceEpoch(timestamp); to var date = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

how to use local flutter package in another flutter application?

Find this file in your flutter application => pubspec.yaml Use local dependency dependencies: flutter: sdk: flutter my_new_package: path: ./my_new_package Note: The ./my_new_package above means that the my_new_package directory containing the pubspec.yaml for the package is a sub-directory of the app. If you have the package as a directory at the same level as the app, … Read more

Flutter SVG rendering

Fonts are a great option for a lot of cases. I’ve been working on a library to render SVGs on a canvas, available here: https://github.com/dnfield/flutter_svg The API as of right now would look something like new SvgPicture.asset(‘asset_name.svg’) to render asset_name.svg (sized to its parent, e.g. a SizedBox). You can also specify a color and blendMode … Read more

What is the relation between stateful and stateless widgets in Flutter?

A StatelessWidget will never rebuild by itself (but can from external events). A StatefulWidget can. That is the golden rule. BUT any kind of widget can be repainted any times. Stateless only means that all of its properties are immutable and that the only way to change them is to create a new instance of … Read more

Flutter get context in initState method

The member variable context can be accessed during initState but can’t be used for everything. This is from the flutter for initState documentation: You cannot use [BuildContext.inheritFromWidgetOfExactType] from this method. However, [didChangeDependencies] will be called immediately following this method, and [BuildContext.inheritFromWidgetOfExactType] can be used there. You could move your initialization logic to didChangeDependencies, however that … Read more

Is there a way to load async data on InitState method?

You can create an async method and call it inside your initState @override void initState () { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_){ _asyncMethod(); }); } _asyncMethod() async { _googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) { setState(() { _currentUser = account; }); }); _googleSignIn.signInSilently(); }

“extends” versus “implements” versus “with”

Extends: Use extends to create a subclass, and super to refer to the superclass. Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc. You use extend if you want to create … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)