views 904 words

https://instabug.com/blog/how-to-release-your-flutter-app-for-ios-and-android/

选中文本;

Shift  +  Alt  + F     实现代码的对齐;

command + . 快速加一层 (开了iterms2就失效) ===> 自定义为

Shift + Ctrl + R

Json to Dart https://javiercbk.github.io/json_to_dart/

Novel:

解决Flutter ListView 或者SingleChildScrollView 嵌套 ListView.builder滑动冲突

原因: SingleChildScrollView 和 ListView 都有滚动属性physics 他们默认是都是可以滚动的, ListView 嵌套 ListView.builder 需要后者shrinkWrap = true,不然报错;

解决方式: 禁用 ListView 的滚动physics 保留 SingleChildScrollView 的滚动 Listview 执行 physics 属性 new NeverScrollableScrollPhysics(), //禁用滚动事件

new ListView.builder(
              shrinkWrap: true,
              physics: new NeverScrollableScrollPhysics(),
)

provider

ChangeNotifier用于向监听器发送通知, 当模型发生改变并且需要更新 UI 的时候可以调用 notifyListeners().

Consumer包裹的widget就是当notifyListeners()被调动时, Consumer包裹的widget的UI就会更新. (最好能把 Consumer 放在 widget 树尽量低的位置上, 不然一点更新就全盘重新构建 widget)

Provider.of. 当需要访问provider里的数据而不要更新UI的时候就可以使用

Provider.of<CartModel>(context, listen: false).removeAll();

4.0:

context.read<T>() // Provider.of<T>(context, listen: false)
context.watch<T>() // Provider.of<T>(context)

1.首先需要导入在pubspec.yaml中导入sqflite

1)dependencies:

sqflite: ^1.0.0

https://pub.dartlang.org/packages/sqflite

2)执行 flutter packages get

3)导入

import 'package:sqflite/sqflite.dart';

2.为了保证对数据库操作的过程中不会创建多个实例,所以工具类需要使用单例模式

主要用到的是DatabaseHelper,数据库的增删查改和sqlite没区别,直接贴出代码块

注:其中使用到的Note.dart

class Note {
  int _id;
  int _pointid;
  String _image;
  int _contractId;
  int _publicationId;
  double _pointLng;
  double _pointLat;
 
  Note(this._pointid, this._image ,this._contractId,this._publicationId,this._pointLng,this._pointLat);
 
  Note.map(dynamic obj) {
    this._id = obj['id'];
    this._pointid = obj['pointid'];
    this._image = obj['image'];
    this._contractId = obj['contractId'];
    this._publicationId = obj['publicationId'];
    this._pointLng = obj['pointLng'];
    this._pointLat = obj['pointLat'];
  }
 
  int get id => _id;
  int get pointid => _pointid;
  String get image => _image;
  int get contractId => _contractId;
  int get publicationId => _publicationId;
  double get pointLng => _pointLng;
  double get pointLat => _pointLat;
 
  Map<String, dynamic> toMap() {
    var map = new Map<String, dynamic>();
    if (_id != null) {
      map['id'] = _id;
    }
    map['pointid'] = _pointid;
    map['image'] = _image;
    map['contractId'] = _contractId;
    map['publicationId'] = _publicationId;
    map['pointLng'] = _pointLng;
    map['pointLat'] = _pointLat;
    return map;
  }
 
  Note.fromMap(Map<String, dynamic> map) {
    this._id = map['id'];
    this._pointid = map['pointid'];
    this._image = map['image'];
    this._contractId = map['contractId'];
    this._pointLng = map['pointLng'];
    this._pointLat = map['pointLat'];
  }
}

1)创建数据库

void _onCreate(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $tableNote($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $pointid TEXT, $image TEXT, $contractId TEXT, $publicationId TEXT, $pointLng TEXT, $pointLat TEXT)');
  }

2)增加一条数据

F

uture<int> saveNote(Note note) async {
    var dbClient = await db;
    var result = await dbClient.insert(tableNote, note.toMap());
//    var result = await dbClient.rawInsert(
//        'INSERT INTO $tableNote ($columnTitle, $columnDescription) VALUES (\'${note.title}\', \'${note.description}\')');
 
    return result;
  }

3)获取数据库所有的数据

Future<List> getAllNotes() async {
    var dbClient = await db;
    var result = await dbClient.query(tableNote, columns: [columnId, pointid, image, contractId ,publicationId, pointLng, pointLat]);
    return result.toList();
  }

4)获取数据库数据的数量

Future<int> getCount() async {
    var dbClient = await db;
    return Sqflite.firstIntValue(await dbClient.rawQuery('SELECT COUNT(*) FROM $tableNote'));
  }

5)获取数据库其中一条数据(键是id)

Future<Note> getNote(int id) async {
    var dbClient = await db;
    List<Map> result = await dbClient.query(tableNote,
        columns: [columnId, pointid, image ,contractId, publicationId, pointLng, pointLat],
        where: '$columnId = ?',
        whereArgs: [id]);
//    var result = await
 dbClient.rawQuery('SELECT * FROM $tableNote WHERE $columnId = $id');
 
    if (result.length > 0) {
      return new Note.fromMap(result.first);
    }
 
    return null;
  }

6)删除数据库一条数据

  Future<int> deleteNote(String images) async {
    var dbClient = await db;
    return await dbClient.delete(tableNote, where: '$image = ?', whereArgs: [images]);
//    return await dbClient.rawDelete('DELETE FROM $tableNote WHERE $columnId = $id');
  }

7)更改数据库一条数据

  Future<int> updateNote(Note note) async {
    var dbClient = await db;
    return await dbClient.update(tableNote, note.toMap(), where: "$columnId = ?", whereArgs: [note.id]);
//    return await dbClient.rawUpdate(
//        'UPDATE $tableNote SET $columnTitle = \'${note.title}\', $columnDescription = \'${note.description}\' WHERE $columnId = ${note.id}');
  }

8)关闭数据库

  Future close() async {
    var dbClient = await db;
    return dbClient.close();
  }

最后贴出代码块的集合

import 'dart:async';
 
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:seller_app/model/Note.dart';
 
class DatabaseHelper {
  static final DatabaseHelper _instance = new DatabaseHelper.internal();
 
  factory DatabaseHelper() => _instance;
 
  final String tableNote = 'note11Table';
  final String columnId = 'id';
  final String pointid = 'pointid';
  final String image = 'image';
  final String contractId = 'contractId';
  final String publicationId = 'publicationId';
  final String pointLat = 'pointLat';
  final String pointLng = 'pointLng';
 
  static Database _db;
 
  DatabaseHelper.internal();
 
  Future<Database> get db async {
    if (_db != null) {
      return _db;
    }
    _db = await initDb();
 
    return _db;
  }
 
  initDb() async {
    String databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'notes11.db');
 
//    await deleteDatabase(path); // just for testing
 
    var db = await openDatabase(path, version: 1, onCreate: _onCreate);
    return db;
  }
 
  void _onCreate(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $tableNote($columnId INTEGER PRIMARY KEY AUTOINCREMENT, $pointid TEXT, $image TEXT, $contractId TEXT, $publicationId TEXT, $pointLng TEXT, $pointLat TEXT)');
  }
 
  Future<int> saveNote(Note note) async {
    var dbClient = await db;
    var result = await dbClient.insert(tableNote, note.toMap());
//    var result = await dbClient.rawInsert(
//        'INSERT INTO $tableNote ($columnTitle, $columnDescription) VALUES (\'${note.title}\', \'${note.description}\')');
 
    return result;
  }
 
  Future<List> getAllNotes() async {
    var dbClient = await db;
    var result = await dbClient.query(tableNote, columns: [columnId, pointid, image, contractId ,publicationId, pointLng, pointLat]);
//    var result = await dbClient.rawQuery('SELECT * FROM $tableNote');
 
    return result.toList();
  }
 
  Future<int> getCount() async {
    var dbClient = await db;
    return Sqflite.firstIntValue(await dbClient.rawQuery('SELECT COUNT(*) FROM $tableNote'));
  }
 
  Future<Note> getNote(int id) async {
    var dbClient = await db;
    List<Map> result = await dbClient.query(tableNote,
        columns: [columnId, pointid, image ,contractId, publicationId, pointLng, pointLat],
        where: '$columnId = ?',
        whereArgs: [id]);
//    var result = await dbClient.rawQuery('SELECT * FROM $tableNote WHERE $columnId = $id');
 
    if (result.length > 0) {
      return new Note.fromMap(result.first);
    }
 
    return null;
  }
 
  Future<int> deleteNote(String images) async {
    var dbClient = await db;
    return await dbClient.delete(tableNote, where: '$image = ?', whereArgs: [images]);
//    return await dbClient.rawDelete('DELETE FROM $tableNote WHERE $columnId = $id');
  }
 
  Future<int> updateNote(Note note) async {
    var dbClient = await db;
    return await dbClient.update(tableNote, note.toMap(), where: "$columnId = ?", whereArgs: [note.id]);
//    return await dbClient.rawUpdate(
//        'UPDATE $tableNote SET $columnTitle = \'${note.title}\', $columnDescription = \'${note.description}\' WHERE $columnId = ${note.id}');
  }
 
  Future close() async {
    var dbClient = await db;
    return dbClient.close();
  }
}

3.使用

1)导入工具类 例:

import 'package:seller_app/systemAssembly/DatabaseHelper.dart';

2)创建

var db = new DatabaseHelper();

3)创建数据:

await db.saveNote(new Note(pointId, _image,contractId,publicationId,locationLng,locationLat));

4)获取所有的数据:

  List notes;
  print('=== getAllNotes() ===');
  notes = await db.getAllNotes();
  notes.forEach((note) => print(note));

5)删除某一条数据:

db.deleteNote(image); //image为数据库中某一条数据的image属性