Creating an instance of a generic type in DART

I tried mezonis approach with the Activator and it works. But it is an expensive approach as it uses mirrors, which requires you to use “mirrorsUsed” if you don’t want to have a 2-4MB js file. This morning I had the idea to use a generic typedef as generator and thus get rid of reflection: … Read more

Display SnackBar in Flutter

In my case i had code like this (in class state) final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); void showInSnackBar(String value) { _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text(value))); } but i didn’t setup the key for scaffold. so when i add key: _scaffoldKey @override Widget build(BuildContext context) { return new Scaffold( key: _scaffoldKey, body: new SafeArea( snackbar start … Read more

flutter run function every x amount of seconds

build() can and usually will be called more than once and every time a new Timer.periodic is created. You need to move that code out of build() like import ‘dart:async’; Timer? timer; @override void initState() { super.initState(); timer = Timer.periodic(Duration(seconds: 15), (Timer t) => checkForNewSharedLists()); } @override void dispose() { timer?.cancel(); super.dispose(); } Even better … Read more

How to flatten a List?

The easiest way I know of is to use Iterable.expand() with an identity function. expand() takes each element of an Iterable, performs a function on it that returns an iterable (the “expand” part), and then concatenates the results. In other languages it may be known as flatMap. So by using an identity function, expand will … Read more

flutter – correct way to create a box that starts at minHeight, grows to maxHeight

There’s no notion of “Starts from max/min size”. The thing is, ContrainedBox only add constraints to it’s child. But in the end, it doesn’t pick a size. If you want your child to hit minSize, then they have to not expend. Which translate into not having a width/height of double.INFINITY. Fact is that double.INFINITY is … Read more

Flutter: Retrieving top-level state from child returns null

TDLR: imports file only using import ‘package:myApp/path/myFile.dart’; Never with import ‘./myFile.dart’; This is due to how dart resolves imports. You may have a single source file, but during builds, there is some kind of duplicates. Let’s say you’re working on ‘myApp’. To import a file, you could do both : import ‘relativePath/myFile.dart’ import ‘package:myApp/path2/myFile.dart’ You’d … Read more

Flutter Test MissingPluginException

If you’re using shared_preferences 0.2.4 and above, use setMockInitialValues: SharedPreferences.setMockInitialValues({}); // set initial values here if desired For earlier versions you can do it manually: const MethodChannel(‘plugins.flutter.io/shared_preferences’) .setMockMethodCallHandler((MethodCall methodCall) async { if (methodCall.method == ‘getAll’) { return <String, dynamic>{}; // set initial values here if desired } return null; });