sql-server - [已更新]如何通过 HTTP POST Flutter 更新后将数据保存在 Microsoft SQL Server 上
问题描述
每次单击“True”按钮时,它都会将值“True”发送到 SQL Server 数据库并更新新值,之后立即重置为“False”。我该如何解决??
[更新]这是我的脚本 MSSQL 服务器更新数据:
USE [dieukhien]
GO
/****** Object: StoredProcedure [dbo].[uodateTrangThaiAppByidMach] Script Date: Tue 20 06 09 12:40:35 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[uodateTrangThaiAppByidMach]
-- Add the parameters for the stored procedure here
@id_mach_dien nvarchar(10) = null,
@trang_thaiapp bit,
@giatri_app bit
AS
BEGIN
update datas set trang_thai_app= @trang_thaiapp, giatri_app=@giatri_app where id_mach_dien=@id_mach_dien
END
主要.dart
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_ap);
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; }
}
}
解决方案
It seems you are returning the same object passed into the API, are you setting the boolean fields to true in API, then you need either return the changed object or set the boolean fields to true in your returning object.
[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();
value.giatri_app = true; //set the field (s) you want to return true as value here
return value; // here you are sending the same object which you are received, no modifications done.
}
推荐阅读
- opencv - OpenCV Knn 匹配错误
- arrays - 理解 Go 的切片符号
- css - 初始颜色...它是如何工作的?
- javascript - React:在类之间传递道具总是未定义的
- c++ - 通过引用传递时访问 std::vector 的元素
- hibernate - “org.hibernate.type.CollectionType:创建的集合包装器”是什么意思?
- heroku - 如何在 Heroku (pointdns) 上配置 DNS
- angular - Wallaby.js - async() 测试助手需要区域
- lua - Lua NodeMCU net.udpsocket:listen - 如何处理套接字已经打开的可能性?
- python - 在 Python 列表中操作元素