'Error: Not a constant expression in FutureBuilder (Flutter) in extracted Widget
I consume event-datas from an API in my flutter-app. The Future-Builder can list the names:
return FutureBuilder(
future: Provider.of<Events>(context, listen: false).fetchAndSetEvents(),
builder: (BuildContext ctx, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
return Consumer<Events>(
builder: (context, eventData, child) => ListView.builder(
itemCount: eventData.items.length,
itemBuilder: (ctx, i) => Column(
children: <Widget>[
Text(eventData.items[i].name),
],
),
),
);
}
},
);
But as soon I change the Text() in my Widget "EventItem" I get the error: Error: Not a constant expression in FutureBuilder (Flutter)
import 'package:flutter/material.dart';
class EventItem extends StatelessWidget {
EventItem(this.name);
final String name;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 5.0),
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: Icon(Icons.calendar_today),
title: Text(name),
),
],
),
),
);
}
}
Where is my error?
Solution 1:[1]
The problem was the "const" before ListTile:
const ListTile()
Solution 2:[2]
Here title: Text(name) is giving issue because you are using a const constructor with ListTile. The compiler is expecting a compile time constant for Text widget and is raising issue because the arguments you are passing in are not constant but depend on the runtime value of name.
If you simply remove the const keyword it will compile without errors.
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 | Alex |
| Solution 2 | Alok Gupta |
