【发布时间】:2021-05-24 08:42:04
【问题描述】:
我正在使用https://pub.dev/packages/slide_digital_clock,它会导致内存泄漏和动画错误垃圾邮件,我不知道如何修复它,一旦视图被发送回注销,就会发生这种情况。如果有人能解释一下如何修复它,将不胜感激
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:attenv02/login_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:geolocator/geolocator.dart';
import 'package:slide_digital_clock/slide_digital_clock.dart';
import 'package:intl/intl.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var locationMessege= "";
bool timeinbtn = true;
bool timeoutbtn = false;
String name = "";
void initState() {
super.initState();
}
Future <String> loadPref()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return Future.delayed(Duration(seconds: 1),()async{
return await sharedPreferences.getString("useFullName");
});
}
logout()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.clear();
sharedPreferences.commit();
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context)
=> LoginPage()), (Route<dynamic> route) => false);
}
void getCurrentLocation()async{
var position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var lastPosition=await Geolocator.getLastKnownPosition();
String now =await new DateFormat.yMd().add_Hm().format(new DateTime.now());
timeinbtn=!timeinbtn;
timeoutbtn=!timeoutbtn;
print(lastPosition);
print(now);
setState(() {
locationMessege="$position.latitude,$position.longitude,$now";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor:Color.fromRGBO(255, 191, 68, 1),
title:FutureBuilder(
future: loadPref(),
builder: (context, snapshot) {
if(snapshot.hasData){
return Text("${snapshot.data}");
}else{
return Text("Loading");
}
},
),
actions: <Widget>[
FlatButton(
onPressed: () {
logout();
},
child: Text("Log Out", style: TextStyle(color: Colors.white)),
),
],
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/bg.jpg"), fit: BoxFit.cover)),
child: Center(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Column(
children:<Widget> [
Container(
child:DigitalClock(
digitAnimationStyle: Curves.elasticOut,
is24HourTimeFormat: false,
areaDecoration: BoxDecoration(
color: Colors.transparent,
),
hourMinuteDigitTextStyle: TextStyle(
color: Colors.blueGrey,
fontSize: 50,
),
amPmDigitTextStyle: TextStyle(color: Colors.blueGrey, fontWeight: FontWeight.bold),
),
),
Text("Position:$locationMessege",style:TextStyle(
color: Colors.black,
fontSize: 10.0,
fontWeight: FontWeight.bold)),
Visibility(
visible:timeinbtn,
child: FlatButton(onPressed:(){
getCurrentLocation();
},
color: Colors.orange,
child: Text("Time in",
style: TextStyle(
color: Colors.white,
),)),
),
Visibility(
visible: timeoutbtn,
child: FlatButton(onPressed:(){
getCurrentLocation();
},
color: Colors.orange,
child: Text("Time out",
style: TextStyle(
color: Colors.white,
),)),
)
],
),
],
),
),
),
),
);
}
}
在最终确定小部件树时引发了以下断言: _SpinnerTextState#321b2(ticker active) 与活动的 Ticker 一起处置。
_SpinnerTextState 通过其 SingleTickerProviderStateMixin 创建了一个 Ticker,但在 mixin 上调用 dispose() 时,该 Ticker 仍然处于活动状态。 Ticker 必须在调用 super.dispose() 之前被释放。
AnimationControllers 使用的代码应该通过调用 AnimationController 本身的 dispose() 来处理。否则,代码会泄露。
动画库捕获的异常 在通知 AnimationController 的侦听器时引发了以下断言: 'package:flutter/src/widgets/framework.dart':断言失败:第 4109 行 pos 12:'_lifecycleState != _ElementLifecycle.defunct':不正确。
动画库捕获的异常 'package:flutter/src/widgets/framework.dart':断言失败:第 4109 行 pos 12:'_lifecycleState != _ElementLifecycle.defunct':不正确。
动画库捕获的异常 'package:flutter/src/widgets/framework.dart':断言失败:第 4109 行 pos 12:'_lifecycleState != _ElementLifecycle.defunct':不正确。
【问题讨论】: