import 'package:flutter/foundation.dart';
import 'package:clubinn/services/network_helper.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
String userToJson(Models data) => json.encode(data.toJson());
class Models {
String url = 'http://localhost:3000';
String name;
String email;
String password;
LocationJson location;
String DOB;
String gender;
String mobile;
String type;
List<double> coordinates;
Models(
{
@required this.email,
@required this.password,
}
);
Future<http.Response> loginDataToJson() async{
Map body = {
"email": email,
"password": password
};
NetworkHelper networkHelper = new NetworkHelper(
'$url/user/login',
);
http.Response response = await networkHelper.postData(body);
//dynamic decodedData = jsonDecode(response.body);
return response;
}
Models.signUp(
{
@required this.name,
@required this.email,
@required this.password,
@required this.location,
@required this.DOB,
@required this.gender,
@required this.mobile
}
);
Map<String, dynamic> toJson() => {
"name": name,
"email": email,
"password": password,
"birthday": DOB,
"gender": gender,
"location": location.toJson(),
"phone_number": mobile,
};
Future<http.Response> createUser() async{
Map<String, dynamic> body = {
"name": name,
"email": email,
"password": password,
"birthday": DOB,
"gender": gender,
"location": location.toJson(),
"phone_number": mobile
};
NetworkHelper networkHelper = new NetworkHelper(
'$url/user/signup',
);
http.Response response = await networkHelper.postData(body);
return response;
}
}
class LocationJson {
String type;
List<double> coordinates;
LocationJson({
this.type,
this.coordinates,
});
Map<String,dynamic> toJson() => {
"type": type,
"coordinates": List<double>.from(coordinates.map((x) => x)),
};
}
我正在尝试创建一个注册模型并将其发送到我的API,但是“位置”键还有另一个map / jsonobject,其中包含“类型”:“点”和“坐标”:[double,double]。
最终的json对象应该看起来像这样
{
"name": "Arsh Bansal",
"email": "[email protected]",
"password": "123456789",
"birthday": "06-21-2000",
"gender": "Male",
"location": {
"type": "Point",
"coordinates": [13.0987, 88.403]
},
"phone_number": "123456789"
}
从注册页面获取数据的代码如下:
class _SignUpPageState extends State<SignUpPage> {
final dateFormat = DateFormat('MM-dd-yyyy');
String name;
String email;
String DOB;
String password;
String mobileNumber;
List<double> coordinates = [];
Location location = Location();
List<String> genderList = ['Male', 'Female', 'Others'];
String selectedGender = 'Male';
var locationTextController = new TextEditingController();
DropdownButton<String> androidDropdown() {
List<DropdownMenuItem<String>> genderDropdown = [];
for(String gender in genderList) {
var newItem = DropdownMenuItem(
child: Text(
gender,
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Color(0xFFDDB911),
),
),
value: gender,
);
genderDropdown.add(newItem);
}
return DropdownButton<String>(
value: selectedGender,
items: genderDropdown,
onChanged: (value) {
selectedGender = value;
},
);
}
CupertinoPicker iOSPicker() {
List<Text> pickerItems = [];
for(String gender in genderList) {
pickerItems.add(
Text(
gender,
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Color(0xFFDDB911),
),
),
);
}
return CupertinoPicker(
backgroundColor: Color(0xFF101010),
itemExtent: 32.0,
onSelectedItemChanged: (selectedIndex) {
selectedGender = genderList[selectedIndex];
},
children: pickerItems,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
SizedBox(height: 5.0),
Align(
alignment: Alignment.topLeft,
child: FlatButton(
onPressed: () {
Navigator.pop(context);
},
child: Icon(
Icons.arrow_back_ios,
size: 50.0,
color: Color(0xFFDDB911),
),
),
),
Container(
padding: EdgeInsets.only(top: 50.0, left: 20.0, right: 20.0),
child: Column(
children: <Widget>[
TextField(
onChanged: (value) {
name = value;
},
decoration: InputDecoration(
labelText: 'NAME',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
),
SizedBox(height: kSizedBoxHeight),
TextField(
onChanged: (value) {
email = value;
},
decoration: InputDecoration(
labelText: 'EMAIL',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
),
SizedBox(height: kSizedBoxHeight),
TextField(
onChanged: (value) {
password = value;
},
decoration: InputDecoration(
labelText: 'PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
obscureText: true,
),
SizedBox(height: kSizedBoxHeight),
TextField(
onChanged: (value) {
mobileNumber = value;
},
decoration: InputDecoration(
labelText: 'MOBILE NUMBER',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
),
SizedBox(height: kSizedBoxHeight),
Row(
children: <Widget>[
Expanded(
flex: 6,
child: TextField(
controller: locationTextController,
decoration: InputDecoration(
labelText: 'ADDRESS',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
),
),
SizedBox(width: 15.0),
Expanded(
flex: 1,
child: GestureDetector(
onTap: () async {
await location.getCurrentLocation();
coordinates = [location.latitude, location.longitude];
setState(() {
locationTextController.text = 'Lat: ${location.latitude}, Long: ${location.longitude}';
});
},
child: Shimmer.fromColors(
baseColor: Color(0xFFDDB911),
highlightColor: Color(0xFFFFFF99),
child: Icon(
Icons.my_location,
size: 50.0,
//color: Color(0xFFDDB911),
),
),
),
),
],
),
SizedBox(height: kSizedBoxHeight),
DateTimeField(
onChanged: (value) {
var currentDate = DateTime.now();
if(value == null) {
value = DateTime.now();
} else if(currentDate.difference(value).inDays < 6575) {
} else {
String dateWithTime = value.toString();
DOB = convertDateTimeDisplay(dateWithTime);
//print(DOB);
}
},
format: dateFormat,
decoration: InputDecoration(
labelText: 'DOB',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Color(0xFFDDB911)),
),
),
onShowPicker: (context, currentValue) {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime.now()
);
},
),
SizedBox(height: 36.0),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'GENDER',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
fontSize: 16.0,
color: Colors.grey,
),
),
Container(
height: 40.0,
alignment: Alignment.center,
color: Color(0xFF101010),
child: Platform.isIOS ? iOSPicker() : androidDropdown(),
),
],
),
SizedBox(height: 40.0),
Container(
height: 50.0,
child: GestureDetector(
onTap: () async {
Models userData = new Models.signUp(
name: name,
email: email,
password: password,
location: LocationJson(type: 'Point', coordinates: coordinates),
DOB: DOB,
gender: selectedGender,
mobile: mobileNumber
);
http.Response response = await userData.createUser();
print(response);
},
child: Material(
borderRadius: BorderRadius.circular(20.0),
shadowColor: Color(0xFFFFFF99),
color: Color(0xFFDDB911),
elevation: 7.0,
child: Center(
child: Text(
'SIGNUP',
style: TextStyle(
color: Color(0xFFEFEFEF),
fontSize: 17.0,
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat'
),
),
),
),
),
),
],
),
),
],
),
),
),
);
}
}
但是我收到下面的错误
我得到的错误是:未处理的异常:类型'_InternalLinkedHashMap>'在类型转换中不是'字符串'类型的子类型
使用此
class Location {
String type;
List<double> coordinates;
Location({this.type, this.coordinates});
Location.fromJson(Map<String, dynamic> json) {
type = json['type'];
coordinates = json['coordinates'].cast<double>();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
data['coordinates'] = this.coordinates;
return data;
}
}