'Error: A value of type 'Widget' can't be assigned to a variable of type 'InheritedWidget'
I am having a problem with my project, there are no errors in my code but when I try to run main.dart it fails to build. The errors it throws are the following:
../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/syncfusion_flutter_gauges-20.1.52/lib/src/linear_gauge/gauge/linear_gauge_scope.dart:40:10: Error: A value of type 'Widget' can't be assigned to a variable of type 'InheritedWidget'.
- 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart').
- 'InheritedWidget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). .widget; ^ ../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/syncfusion_flutter_gauges-20.1.52/lib/src/radial_gauge/pointers/marker_pointer_renderer.dart:607:22: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/binding.dart'). WidgetsBinding.instance!
FAILURE: Build failed with an exception.
Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1156
What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.
Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
This is the code that uses the radial gauge package:
enter code here
// @dart=2.9
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_gauges/gauges.dart';
import 'package:http/http.dart' as http;
import 'Readings.dart';
class DashBoardPage extends StatefulWidget {
const DashBoardPage({Key key}) : super(key: key);
@override
State<DashBoardPage> createState() => _DashBoardPageState();
}
class _DashBoardPageState extends State<DashBoardPage> {
final String url = "http://mushroomdroid.online/dbscript-1.php";
List<Readings> AllData = [];
@override
void initState() {
super.initState();
loadData();
}
loadData() async {
var response =
await http.get(Uri.parse(url), headers: {"Accept": "application/json"});
if (response.statusCode == 200) {
String responseBody = response.body;
var jsonBody = json.decode(responseBody);
for (var data in jsonBody) {
AllData.add(Readings(
int.parse(data['id']),
double.parse(data['temperature']),
double.parse(data['humidity']),
data['FanStatus'],
data['MistStatus'],
DateTime.parse(data['Time'])));
}
setState(() {});
//AllData.forEach((someData) => print("temperature: ${someData.temperature}"));
} else {
print('Something went wrong');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: AllData.isEmpty
? Center(
child: CircularProgressIndicator(),
)
: showMyUI(),
));
}
Widget showMyUI() {
return ListView.builder(
itemCount: 1,
itemBuilder: (_, index) {
return Card(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/whitelast.jpg'),
fit: BoxFit.cover,
)),
padding: EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Dashboard",
style: TextStyle(fontFamily: 'OpenSans', fontSize: 25)),
Text("Latest reading for today,\n${AllData.first.petsa}"),
SfRadialGauge(axes: <RadialAxis>[
RadialAxis(
startAngle: 180,
endAngle: 360,
showTicks: false,
showLabels: false,
radiusFactor: 0.8,
pointers: <GaugePointer>[
RangePointer(
value: AllData.last.temperature,
cornerStyle: CornerStyle.bothCurve,
width: 10,
sizeUnit: GaugeSizeUnit.logicalPixel,
gradient: const SweepGradient(colors: <Color>[
Color(0xFFffd800),
Color(0xFFffba00)
], stops: <double>[
0.25,
0.75
])),
],
annotations: <GaugeAnnotation>[
GaugeAnnotation(
positionFactor: 0.7,
angle: 90,
widget: Column(
children: <Widget>[
Container(
width: 100.00,
height: 100.00,
decoration: BoxDecoration(
image: DecorationImage(
image: ExactAssetImage(
'assets/qwe.png'),
),
)),
Padding(
padding: EdgeInsets.fromLTRB(0, 2, 0, 0),
child: Text(
' ${AllData.first.temperature}\n Temperature',
style: TextStyle(
fontFamily: 'OpenSans',
fontWeight: FontWeight.normal,
fontSize: 25)),
)
],
),
)
])
]),
SfRadialGauge(axes: <RadialAxis>[
RadialAxis(
startAngle: 180,
endAngle: 360,
annotations: <GaugeAnnotation>[
GaugeAnnotation(
positionFactor: 0.7,
angle: 90,
widget: Column(
children: <Widget>[
Container(
width: 70.00,
height: 70.00,
decoration: BoxDecoration(
image: DecorationImage(
image:
AssetImage('assets/images(2).jpg'),
))),
Padding(
padding: EdgeInsets.fromLTRB(0, 2, 0, 0),
child: Text(
' ${AllData.first.humidity}\n Humidity',
style: TextStyle(
fontWeight: FontWeight.normal,
fontSize: 25)),
)
],
))
],
radiusFactor: 0.8,
showLabels: false,
showTicks: false,
pointers: <GaugePointer>[
RangePointer(
value: AllData[index].humidity,
cornerStyle: CornerStyle.bothCurve,
width: 10,
sizeUnit: GaugeSizeUnit.logicalPixel,
gradient: const SweepGradient(colors: <Color>[
Color(0xFF00FFFF),
Color(0xFF6495ED)
], stops: <double>[
0.25,
0.75
])),
],
)
])
],
),
),
);
});
}
}
Solution 1:[1]
This was caused by a breaking change in the Flutter framework. Specifically, InheritedElement.widget now returns a Widget instead of an InheritedWidget, with downcasts occurring at call sites where needed. The gauges library assigns this to an InheritedWidget variable without casting, which causes a compile-time error.
The change was introduced in this commit, and is in all versions on the beta channel since 2.12.0-4.1.pre.
Your options are to downgrade Flutter to an earlier version, fork the library to remove the unnecessary InheritedWidget type annotation, or hope that either the Flutter framework or the gauges library is changed in the future. The simplest option is probably to switch to the Flutter stable channel, which is at version 2.10.5 at this time.
> flutter channel stable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nitrodon |
