问题描述
在我使用 SignalR 和 Provider 的项目中,我使用 signalR 拉取数据。我可以在调试模式和模拟器中看到它。我是新来的
我的问题是,在应用程序中,我可以立即通过 Provider 监听更改,相关小部件会重建整个页面,而不是重建它。
如何让它重建只有数据在监听的小部件。
我的视图模型、视图和小部件代码如下。
我是 Flutter 的新手,所以如果您有任何遗漏,我将不胜感激。
主页代码:
import 'package:cobasys_mobile/pages/drawer_menu.dart';
import 'package:cobasys_mobile/viewmodel/santral_viewmodel.dart';
import 'package:cobasys_mobile/viewmodel/user_viewmodel.dart';
import 'package:cobasys_mobile/widgets/ozet_kutu_widget.dart';
import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
debugPrint("HomePage rebuild");
return Scaffold(
appBar: AppBar(
title: Text("Giriş"),actions: [
TextButton(
onpressed: () => _cikisYap(context),child: Text(
"Çıkış Yap",style: TextStyle(color: Colors.white),),],drawer: MainDrawer(),body: Column(
children: [
Text(
"Günlük Özet Bilgiler",style: TextStyle(
fontSize: 18,fontWeight: FontWeight.w500,SizedBox(
height: 15,Row(
children: [
OzetKutu(
icon_ozet: Icons.corporate_fare,color_ozet: Colors.lightBlue,title_ozet: "Üretilen",subtitle_ozet: context.watch<Santralviewmodel>().uretimOzet.gunlukUretim + " m3"),OzetKutu(
icon_ozet: Icons.assignment_rounded,color_ozet: Colors.brown,title_ozet: "Bekleyen Üretim",subtitle_ozet: context.watch<Santralviewmodel>().uretimOzet.bekleyenUretim + " m3"),);
}
Future<void> _cikisYap(BuildContext context) async {
final _santralModel = Provider.of<Santralviewmodel>(context,listen: false);
await _santralModel.santralSignOut();
final _usermodel = Provider.of<Userviewmodel>(context,listen: false);
await _usermodel.signOut();
}
}
视图模型代码:
import 'package:cobasys_mobile/locator.dart';
import 'package:cobasys_mobile/models/signalr_santral_model.dart';
import 'package:cobasys_mobile/services/signalr_santral_service.dart';
import 'package:Flutter/material.dart';
class Santralviewmodel with ChangeNotifier {
SignalrsantralService _signalrsantralService = locator<SignalrsantralService>();
UretimOzet _uretimOzet;
set uretimOzet(UretimOzet value) {
_uretimOzet = value;
notifyListeners();
}
UretimOzet get uretimOzet {
if (_uretimOzet == null) {
return UretimOzet(
gunlukUretim: "",bekleyenUretim: "",uretilenSiparis: "",bekleyenSiparis: "",santralVerim: "");
} else {
return _uretimOzet;
}
}
Santralviewmodel() {
_signalrsantralService.signalrsantralConnection();
buildSantral();
}
Future<void> buildSantral() async {
debugPrint("SignalrsantralService Başlatılıyor...");
_signalrsantralService.connection.on("UretimOzet",(data) {
if (data != null) {
uretimOzet = UretimOzet.fromJson(data[0]);
debugPrint(uretimOzet.gunlukUretim);
}
});
}
Future<bool> santralSignOut() {
uretimOzet = null;
_signalrsantralService.connection.stop();
debugPrint("Santral connection kapatıldı.");
return Future.value(true);
}
}
我的小部件代码:
import 'package:Flutter/material.dart';
class OzetKutu extends StatelessWidget {
final IconData icon_ozet;
final Color color_ozet;
final String title_ozet;
final String subtitle_ozet;
const OzetKutu({Key key,this.icon_ozet,this.color_ozet,this.title_ozet,this.subtitle_ozet})
: super(key: key);
@override
Widget build(BuildContext context) {
debugPrint("Ozet Kutu reBuild");
return Container(
width: MediaQuery.of(context).size.width / 2,child: Card(
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,children: [
Padding(
padding: const EdgeInsets.all(10.0),child: Container(
child: Icon(
icon_ozet,size: 50,color: color_ozet,Expanded(
child: Container(
height: 70,child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: [
Text(
title_ozet,style: TextStyle(fontSize: 14,fontWeight: FontWeight.w400),Text(
subtitle_ozet,style: TextStyle(fontSize: 18,fontWeight: FontWeight.w600),);
}
}
型号代码:
// To parse this JSON data,do
//
// final uretimOzet = uretimOzetFromJson(jsonString);
import 'dart:convert';
UretimOzet uretimOzetFromJson(String str) => UretimOzet.fromJson(json.decode(str));
String uretimOzetToJson(UretimOzet data) => json.encode(data.toJson());
class UretimOzet {
UretimOzet({
this.gunlukUretim,this.bekleyenUretim,this.uretilenSiparis,this.bekleyenSiparis,this.santralVerim,this.enerjiTuketim,});
String gunlukUretim;
String bekleyenUretim;
String uretilenSiparis;
String bekleyenSiparis;
String santralVerim;
String enerjiTuketim;
factory UretimOzet.fromJson(Map<String,dynamic> json) => UretimOzet(
gunlukUretim: json["gunlukUretim"] == null ? null : json["gunlukUretim"].toString(),bekleyenUretim: json["bekleyenUretim"] == null ? null : json["bekleyenUretim"].toString(),uretilenSiparis:
json["uretilenSiparis"] == null ? null : json["uretilenSiparis"].toString(),bekleyenSiparis:
json["bekleyenSiparis"] == null ? null : json["bekleyenSiparis"].toString(),santralVerim: json["santralVerim"] == null ? null : json["santralVerim"].toString(),enerjiTuketim: json["enerjiTuketim"] == null ? null : json["enerjiTuketim"].toString(),);
Map<String,dynamic> toJson() => {
"gunlukUretim": gunlukUretim == null ? null : gunlukUretim,"bekleyenUretim": bekleyenUretim == null ? null : bekleyenUretim,"uretilenSiparis": uretilenSiparis == null ? null : uretilenSiparis,"bekleyenSiparis": bekleyenSiparis == null ? null : bekleyenSiparis,"santralVerim": santralVerim == null ? null : santralVerim,"enerjiTuketim": enerjiTuketim == null ? null : enerjiTuketim,};
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)