每次我单击“ True”按钮时,它都会在SQL Server数据库上发送值“ True”并更新新值,此后立即返回“ False”。我该如何解决??
Future<UserModel> createUser(String id, bool trangthaiapp, bool giatriapp) async{ final response = await http.post(apiUrl, headers: headers, body: json.encode({ "id_machdien": id, "trang_thaiapp": trangthaiapp, "giatri_app:": giatriapp, }) ); if(response.statusCode == 200){ final String responseString = response.body; return userModelFromJson(responseString); }else{ return null; } } class _MyHomePageState extends State<MyHomePage> { UserModel _user; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text(widget.title), ), body:Center( child: Column( children: <Widget> [ RaisedButton( onPressed: () async{ final String id = "333"; final bool trangthaiapp = true; final bool giatriapp= true; final UserModel user = await createUser(id, trangthaiapp, giatriapp); setState(() { _user = user; }); }, ), RaisedButton( onPressed: () async{ final String id = "333"; final bool trangthaiapp = false; final bool giatriapp= false; final UserModel user = await createUser(id, trangthaiapp, giatriapp); setState(() { _user = user; }); }, ), ], ), ), ) ); } }
关于类user_model.dart
import 'dart:convert'; UserModel userModelFromJson(String str) => UserModel.fromJson(json.decode(str)); String userModelToJson(UserModel data) => json.encode(data.toJson()); class UserModel { String id; String trangthaiapp; String giatriapp; UserModel({ this.id, this.trangthaiapp, this.giatriapp, }); factory UserModel.fromJson(Map<String, dynamic> json) { var userModel = UserModel( id: json["id_machdien"], trangthaiapp: json["trang_thaiapp"], giatriapp: json["giatri_app"], ); return userModel; } Map<String, dynamic> toJson() => { "id_machdien": id, "trang_thaiapp": trangthaiapp, "giatri_app": giatriapp, }; }
这是我的API代码项目:
ValuesController.cs
using API.IOT.Models; using BasicAuthentication; using DocumentFormat.OpenXml.Drawing.Charts; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace API.IOT.Controllers { public class ValuesController : ApiController { // GET api/values public object Get(MachDien value) { using (dieukhienEntities db = new dieukhienEntities()) { //return db.getDataByidMach(value.id_machdien).ToString(); var entity = db.getDataByidMach(value.id_machdien).FirstOrDefault(); return entity; } } // POST api/values [BasicAuthentication] public object Post(MachDien value) { dieukhienEntities db = new dieukhienEntities(); int save = db.uodateTrangThaiBorByidMach(value.id_machdien,value.trang_thai,value.giatri_bor); int saveapp = db.uodateTrangThaiAppByidMach(value.id_machdien,value.trang_thaiapp,value.giatri_app); getDataByidMach_Result item = db.getDataByidMach(value.id_machdien).FirstOrDefault(); return value; } } }
这是我的Models.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace API.IOT.Models
{
public class MachDien
{
public string id_machdien { get; set; }
public bool trang_thai { get; set; }
public bool trang_thaiapp { get; set; }
public bool giatri_app { get; set; }
public bool giatri_bor { get; set; }
}
}
每次我单击“ True”按钮时,它都会在SQL Server数据库上发送值“ True”并更新新值,此后立即返回“ False”。我该如何解决??未来<...>
似乎您正在返回传递给API的同一对象,是否在API中将布尔字段设置为true,然后需要返回更改后的对象或在返回对象中将布尔字段设置为true。