// // flutter/dart cheatsheet // ///////////////////////////////////////////////////////// flutter changing channels How to change channels You can see which channel you're on with the following command: $ flutter channel Flutter channels: * stable beta dev master To switch channels, run flutter channel [], and then run flutter upgrade to ensure you're on the latest. /////////////////////////////////////////////////////////// try your flutter / dart code online https://dartpad.dev/ //////////////////////////////////////////////////////////////// dart inheritance syntax class Location { num lat, lng; // instance variables Location(this.lat, this.lng); // generative constructor } class ElevatedLocation extends Location { // Extended Location class num elevation; // instance variable ElevatedLocation(num lat, num lng, this.elevation) : super(lat, lng); // call Location(...) } main() { var location = new ElevatedLocation(21.271488, -157.822806, 20); print("location=${location.lat},${location.lng},${location.elevation}"); } //////////////////////////////////////////////////////////////// flutter_blue Connecting to a device without scan #569 I have done it this way: protos.BluetoothDevice dev = protos.BluetoothDevice()..remoteId = address; var _gattClient = BluetoothDevice.fromProto(dev); var _gattClient.connect(autoConnect: everConnectedDevicesAddresses.contains(address)); Note: be carefull with autoConnect option. I just fixed a big issue on LG phones... I could not autoconnect after bluetooth reset. So I made some Set of connected devices, that I am clearing when bluetooth is turning off or turned off. autoconnect parameter should be true only if device was connected at least once after bluetooth or phone reset. /////////////////////////////////////////////////////////////////// // Provider outside widget tree without buildcontext typedef Read = T Function(); class Model { Model(this._read); final Read _read; void method(){ final value = _read>(); } } Provider( create: (context) = _read(); child: ..., } ///////////////////////////////////////////////////////////////// // without cascade d1.setA(20); d1.setB(25); d1.showVal(); // with cascade d1..setA(20) ..setB(25) ..showVal(); //////////////////////////////////////////////////////////////////// int mark = 10; int total = 10; int amount = 10; But if you are lazy like me than you can also do these things this way int mark =10, total = 20, amount = 30; ////////////////////////////////////////////////////////// If you want to prevent unwanted widget rebuilds always use const constructor. In the code below, the instance of BoxDecoration will remain the same even if the setState is called. Container( width: 250, height: 250, decoration: const BoxDecoration( borderRadius: BorderRadius.only( bottomLeft: const Radius.circular(30.0), bottomRight: const Radius.circular(30.0))), child:..... ); ////////////////////////////////////////////////////////// vs editor shortcuts/tips F5 - start debugging Ctrl + Shift + F5 - restart debugging Ctrl + F5 - hot restart Alt + Shift + f - fix indentation and format code, make sure add comma Ctrl + Tab, hold down Ctrl keep pressing Tab - switch to file/tab Ctrl + Shift + \ - find bracket pair Shift + Alt + -> - find firt bracket/paren expand/shrink selection Ctrl + g - goto line number Ctrl + space - pop up menu stanim - create stateful widget with animation controller stful - create stateful widget and it's subclass stless - create stateless widget //////////////////////////////////////////////////////// to test flutter release apk: start from your project root folder command -> flutter build apk --split-per-abi cd build/app/outputs/apk/release python3 -m http.server on your phone or tablet go to chrome type in url 192.168.5.23:8000 // or whatever is the ip of your dev machine download the right apk then install amazon fire tablet 7 - arm 32 bit amazon fire tablet 8 - arm 32 bit amazon fire tablet 10 - arm 64 bit acer chromebook 2 in 1 - arm 32 bit, even though it's an Intel chip samsung galaxy s8+ - arm 64 bit samsung galaxy s10 - arm 64 bit ////////////////////////////////////////// when using amazon fire for usb debugging I get no permissions (user in plugdev group; are your udev rules wrong solution from stackoverflow With my Pixel phone plug into the computer, in the notification shade, I changed it from USB "Charge this phone" to "PTP" (Picture Transfer Protocol). I then ran flutter doctor and my Pixel asked me to "Allow USB debugging" for my computer, I ticked the box to "Always allow from this computer". ///////////////////////////////////////// hide status bar import 'package:flutter/services.dart'; void main() { SystemChrome.setEnabledSystemUIOverlays([]); } /////////////////////////////////////////// lock orientation import 'package:flutter/services.dart'; void main() async { await SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, ]); runApp(App()); } ////////////////////////////////////////////// widget refactor constant: ----------------------------------- Row(children:[ // separate file calling container container, final container = Container( // separate file color: .. method: --------------------------------------- Row(children:[ // separate file calling container container(), Widget container(){ // separate file uses Widget return return Container( color: ... another method: Container container(){ // separate file uses Container return return Container( color: ... widget class: ------------------------------------------ Row(children:[ // separate file calling container const MyContainer(), class MyContainer extends StatlessWidget { // separate file const MyContainer({Key key}):super(key: key); @override Widget build(BuildContext bc) { return Container( color: ... ////////////// another refactor found on stackoverflow import 'package:flutter/material.dart'; AppBar headerNav({String title}){ return AppBar( title: Text(title), ); } return Scaffold( appBar: headerNav(text:'Home Page'), body: Container( child: Text('Home'), ), ///////////////////////////////////////// generate list of widgets child: Column(children:[ for (var widget in widgetList) Text(widget.toString()) // Text can be any widget, no braces on the for loop, no return keyword //////////////////////////////////////////// function callback without return value 1st file: callBack(){ setState((){});} refresh ui Widget1(callBack: callBack); 2nd file: final VoidCallback callback; Widget1({this.callBack}); // constructor @override Widget build.. retrun someWidget ... some button on the 2nd file onPressed:() => callBack(), -- function from file 1 ///////////////////////////////////////////////////// function callback with return value 1st file: Widget1( onValueChange:(int val) { setState(() => do something with val); 2nd file: final Function(int) onValueChange; Widget1({this.onValueChange}); @override Widget build ... some button on the 2nd file onPressed:() { onValueChange(1) } /////////////////////////////////////////////////////// Screen size - MediaQuery.of(context).size.width and height hide keyboard - FocusScope.of(context).requestFocus(FocusNode()); b ??= val; If b is null, assign the value of val to b; otherwise, b stays the same. a = value ?? 0; If value is null, set a to 0. Otherwise, set a to value. a?.b Conditional access. Equivalent: a == null ? null : a.b