Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null safety #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions address_picker/lib/address_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,35 @@ import 'address_model.dart';

abstract class AddressManager {

static List<AddressProvince> _provinces;
static List<AddressProvince>? _provinces;
static Map<String, AddressProvince> _provinceMap = Map<String, AddressProvince>();
static Map<String, AddressCity> _cityMap = Map<String, AddressCity>();
static Map<String, AddressDistrict> _districtMap = Map<String, AddressDistrict>();

static Future<List<AddressProvince>> loadAddressData (
BuildContext context) async {

if (_provinces != null && _provinces.isNotEmpty) {
return _provinces;
if (_provinces != null && (_provinces?.isNotEmpty ?? false)) {
return _provinces!;
}
var address = await rootBundle.loadString('packages/address_picker/assets/address.json');
var data = json.decode(address);
var provinces = new List<AddressProvince>();
if (json != null && data is List) {
var provinces = <AddressProvince>[];
if (data is List) {
data.forEach((v) {
var province = AddressProvince.fromJson(v, cityMap: _cityMap, districtMap: _districtMap);
_provinceMap[province.provinceid] = province;
if (province.provinceid != null) {
_provinceMap[province.provinceid!] = province;
}
provinces.add(province);
});
_provinces = provinces;
return _provinces;
return _provinces!;
}
return List<AddressProvince>();
return <AddressProvince>[];
}

static Future<AddressProvince> getProvince(BuildContext context, String provinceId) async {
static Future<AddressProvince?> getProvince(BuildContext context, String provinceId) async {
if (_provinceMap.isEmpty) {
var provinces = await loadAddressData(context);
if (provinces.isNotEmpty) {
Expand All @@ -43,7 +45,7 @@ abstract class AddressManager {
}
}

static Future<AddressCity> getCity(BuildContext context, String cityId) async {
static Future<AddressCity?> getCity(BuildContext context, String cityId) async {
if (_cityMap.isEmpty) {
var provinces = await loadAddressData(context);
if (provinces.isNotEmpty) {
Expand All @@ -55,7 +57,7 @@ abstract class AddressManager {
}
}

static Future<AddressDistrict> getDistrict(BuildContext context, String districtId) async {
static Future<AddressDistrict?> getDistrict(BuildContext context, String districtId) async {
if (_districtMap.isEmpty) {
var provinces = await loadAddressData(context);
if (provinces.isNotEmpty) {
Expand Down
71 changes: 39 additions & 32 deletions address_picker/lib/address_model.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class AddressProvince {
int id;
String provinceid;
String province;
String lng;
String lat;
List<AddressCity> cities;
int? id;
String? provinceid;
String? province;
String? lng;
String? lat;
List<AddressCity>? cities;

AddressProvince(
{this.id,
Expand All @@ -14,44 +14,46 @@ class AddressProvince {
this.lat,
this.cities});

AddressProvince.fromJson(Map<String, dynamic> json, {Map<String, AddressCity> cityMap, Map<String, AddressDistrict> districtMap}) {
AddressProvince.fromJson(Map<String, dynamic> json, {Map<String, AddressCity>? cityMap, Map<String, AddressDistrict>? districtMap}) {
id = json['id'];
provinceid = json['provinceid'];
province = json['province'];
lng = json['lng'];
lat = json['lat'];
if (json['cities'] != null) {
cities = new List<AddressCity>();
cities = <AddressCity>[];
json['cities'].forEach((v) {
var city = AddressCity.fromJson(v, districtMap: districtMap);
cityMap[city.cityid] = city;
cities.add(city);
if (city.cityid != null) {
cityMap?[city.cityid!] = city;
}
cities!.add(city);
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['id'] = this.id;
data['provinceid'] = this.provinceid;
data['province'] = this.province;
data['lng'] = this.lng;
data['lat'] = this.lat;
if (this.cities != null) {
data['cities'] = this.cities.map((v) => v.toJson()).toList();
data['cities'] = this.cities!.map((v) => v.toJson()).toList();
}
return data;
}
}

class AddressCity {
int id;
String city;
String cityid;
String provinceid;
String lng;
String lat;
List<AddressDistrict> district;
int? id;
String? city;
String? cityid;
String? provinceid;
String? lng;
String? lat;
List<AddressDistrict>? district;

AddressCity(
{this.id,
Expand All @@ -62,45 +64,50 @@ class AddressCity {
this.lat,
this.district});

AddressCity.fromJson(Map<String, dynamic> json, {Map<String, AddressDistrict> districtMap}) {
AddressCity.fromJson(Map<String, dynamic> json, {Map<String, AddressDistrict>? districtMap}) {
id = json['id'];
city = json['city'];
cityid = json['cityid'];
provinceid = json['provinceid'];
lng = json['lng'];
lat = json['lat'];
if (json['district'] != null) {
district = new List<AddressDistrict>();
district = <AddressDistrict>[];
if (districtMap == null) {
districtMap = Map<String, AddressDistrict>();
}
json['district'].forEach((v) {
var dis = AddressDistrict.fromJson(v);
districtMap[dis.areaid] = dis;
district.add(dis);
if (dis.areaid != null) {
districtMap![dis.areaid!] = dis;
}
district!.add(dis);
});
}
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['id'] = this.id;
data['city'] = this.city;
data['cityid'] = this.cityid;
data['provinceid'] = this.provinceid;
data['lng'] = this.lng;
data['lat'] = this.lat;
if (this.district != null) {
data['district'] = this.district.map((v) => v.toJson()).toList();
data['district'] = this.district?.map((v) => v.toJson()).toList();
}
return data;
}
}

class AddressDistrict {
int id;
String area;
String areaid;
String cityid;
String lng;
String lat;
int? id;
String? area;
String? areaid;
String? cityid;
String? lng;
String? lat;

AddressDistrict({this.id, this.area, this.areaid, this.cityid, this.lng, this.lat});

Expand All @@ -114,7 +121,7 @@ class AddressDistrict {
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
final Map<String, dynamic> data = Map<String, dynamic>();
data['id'] = this.id;
data['area'] = this.area;
data['areaid'] = this.areaid;
Expand Down
56 changes: 29 additions & 27 deletions address_picker/lib/address_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:flutter/material.dart';
import 'address_model.dart';

class Address {
AddressProvince currentProvince;
AddressCity currentCity;
AddressDistrict currentDistrict;
AddressProvince? currentProvince;
AddressCity? currentCity;
AddressDistrict? currentDistrict;

Address({this.currentProvince, this.currentCity, this.currentDistrict});
}
Expand All @@ -24,7 +24,7 @@ enum AddressPickerMode {
class AddressPicker extends StatefulWidget {

/// 选中的地址发生改变回调
final AddressCallback onSelectedAddressChanged;
final AddressCallback? onSelectedAddressChanged;

/// 选择模式
/// province 一级: 省
Expand All @@ -36,7 +36,7 @@ class AddressPicker extends StatefulWidget {
final TextStyle style;

AddressPicker(
{Key key,
{Key? key,
this.mode = AddressPickerMode.provinceCityAndDistrict,
this.onSelectedAddressChanged,
this.style = const TextStyle(color: Colors.black, fontSize: 17)})
Expand All @@ -46,11 +46,11 @@ class AddressPicker extends StatefulWidget {
}

class _AddressPickerState extends State<AddressPicker> {
List<AddressProvince> _provinces;
List<AddressProvince>? _provinces;

AddressProvince _selectedProvince;
AddressCity _selectedCity;
AddressDistrict _selectedDistrict;
AddressProvince? _selectedProvince;
AddressCity? _selectedCity;
late AddressDistrict _selectedDistrict;

FixedExtentScrollController _cityScrollController =
FixedExtentScrollController(initialItem: 0);
Expand All @@ -74,9 +74,9 @@ class _AddressPickerState extends State<AddressPicker> {
var addressData = await AddressManager.loadAddressData(context);
setState(() {
_provinces = addressData;
_selectedProvince = _provinces.first;
_selectedCity = _selectedProvince.cities.first;
_selectedDistrict = _selectedCity.district.first;
_selectedProvince = _provinces?.first ?? AddressProvince();
_selectedCity = _selectedProvince?.cities?.first ?? AddressCity();
_selectedDistrict = _selectedCity?.district?.first ?? AddressDistrict();
});
}

Expand All @@ -86,13 +86,15 @@ class _AddressPickerState extends State<AddressPicker> {
currentProvince: _selectedProvince,
currentCity: _selectedCity,
currentDistrict: _selectedDistrict);
widget.onSelectedAddressChanged(address);
if (widget.onSelectedAddressChanged != null) {
widget.onSelectedAddressChanged!(address);
}
}
}

@override
Widget build(BuildContext context) {
if (_provinces == null || _provinces.isEmpty) {
if (_provinces == null || (_provinces?.isEmpty ?? true)) {
return Container();
}

Expand All @@ -106,21 +108,21 @@ class _AddressPickerState extends State<AddressPicker> {
backgroundColor: Colors.white,
childCount: _provinces?.length ?? 0,
itemBuilder: (context, index) {
var item = _provinces[index];
var item = _provinces?[index];
return Container(
alignment: Alignment.center,
child: Text(
item.province,
item?.province ?? "未知省份",
style: widget.style,
),
);
},
itemExtent: 44,
onSelectedItemChanged: (item) {
setState(() {
_selectedProvince = _provinces[item];
_selectedCity = _selectedProvince.cities.first;
_selectedDistrict = _selectedCity.district.first;
_selectedProvince = _provinces?[item] ?? AddressProvince();
_selectedCity = _selectedProvince?.cities?.first ?? AddressCity();
_selectedDistrict = _selectedCity?.district?.first ?? AddressDistrict();
_cityScrollController.animateToItem(0,
curve: Curves.easeInOut,
duration: Duration(milliseconds: 250));
Expand All @@ -141,20 +143,20 @@ class _AddressPickerState extends State<AddressPicker> {
backgroundColor: Colors.white,
childCount: _selectedProvince?.cities?.length ?? 0,
itemBuilder: (context, index) {
var item = _selectedProvince.cities[index];
var item = _selectedProvince?.cities?[index];
return Container(
alignment: Alignment.center,
child: Text(
item.city,
item?.city ?? "未知城市",
style: widget.style,
),
);
},
itemExtent: 44,
onSelectedItemChanged: (item) {
setState(() {
_selectedCity = _selectedProvince.cities[item];
_selectedDistrict = _selectedCity.district.first;
_selectedCity = _selectedProvince?.cities?[item] ?? AddressCity();
_selectedDistrict = _selectedCity?.district?.first ?? AddressDistrict();
_districtScrollController.animateToItem(0,
curve: Curves.easeInOut,
duration: Duration(milliseconds: 250));
Expand All @@ -171,19 +173,19 @@ class _AddressPickerState extends State<AddressPicker> {
backgroundColor: Colors.white,
childCount: _selectedCity?.district?.length ?? 0,
itemBuilder: (context, index) {
var item = _selectedCity.district[index];
var item = _selectedCity?.district?[index];
return Container(
alignment: Alignment.center,
child: Text(
item.area,
item?.area ?? "未知地区",
style: widget.style,
),
);
},
itemExtent: 44,
onSelectedItemChanged: (item) {
var district = _selectedCity.district[item];
_selectedDistrict = district;
var district = _selectedCity?.district?[item];
_selectedDistrict = district ?? AddressDistrict();
_updateCurrent();
},
),
Expand Down
4 changes: 2 additions & 2 deletions address_picker/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: address_picker
description: Flutter城市选择器, 省市区选择器
version: 0.0.1
version: 1.0.0
author: chendaxin <[email protected]>
homepage: https://github.com/SiriDx/address_picker

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
Expand Down