Flutter: Access Stored Sharedpreference value from Other Pages

Flutter shared preferences is actually implemented as an in-memory cache. The first time that you call SharedPreferences.getInstance() all the current values are read from NSUserDefaults (on iOS) and SharedPreferences (on Android) and cached in memory. This involves channels, so is async. The Future returns a singleton class that wraps this cache. Any subsequent calls to … Read more

How to read local json import in flutter?

You should look into loading assets in flutter. You can’t simply import an arbitrary file. Importing is for source code/libraries. You need to declare this file as an asset in your pubspec.yaml flutter: assets: – json_data.json Then in your code you can load this asset as a String: import ‘package:flutter/services.dart’ show rootBundle; Future<String> getJson() { … Read more

how to implement a sliverAppBar with a tabBar

Use NestedScrollView, here is the working code. @override Widget build(BuildContext context) { return Scaffold( body: DefaultTabController( length: 2, child: NestedScrollView( headerSliverBuilder: (context, value) { return [ SliverAppBar( bottom: TabBar( tabs: [ Tab(icon: Icon(Icons.call), text: “Call”), Tab(icon: Icon(Icons.message), text: “Message”), ], ), ), ]; }, body: TabBarView( children: [ CallPage(), MessagePage(), ], ), ), ), ); … Read more

How to convert asset image to File?

You can access the byte data via rootBundle. Then, you can save it to the device’s temporary directory which is obtained by path_provider (you need to add it as a dependency). import ‘dart:async’; import ‘dart:io’; import ‘package:flutter/services.dart’ show rootBundle; import ‘package:path_provider/path_provider.dart’; Future<File> getImageFileFromAssets(String path) async { final byteData = await rootBundle.load(‘assets/$path’); final file = File(‘${(await … Read more

Flutter floating action button with speed dial

Here’s a sketch of how to implement a Speed dial using FloatingActionButton. import ‘package:flutter/material.dart’; import ‘dart:math’ as math; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State createState() => new MyHomePageState(); } … Read more

Multiple widgets used the same GlobalKey

Could you create your keys “by hand” and use static/constant values? e.g. … import ‘package:flutter/widgets.dart’; class RIKeys { static final riKey1 = const Key(‘__RIKEY1__’); static final riKey2 = const Key(‘__RIKEY2__’); static final riKey3 = const Key(‘__RIKEY3__’); } then in … body: new TabBarView( children: [ new RefreshIndicator(new RefreshIndicator( // Use the Manual Static Value instead … Read more

How do I crop an image in Flutter?

I would probably use a BoxDecoration with a DecorationImage. You can use the alignment and fit properties to determine how your image is cropped. You can use an AspectRatio widget if you don’t want to hard code a height on the Container. import ‘package:flutter/material.dart’; void main() { runApp(new MaterialApp( home: new MyHomePage(), )); } class … Read more

My async call is returning before list is populated in forEach loop

This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more