Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Last active March 3, 2020 11:02
Show Gist options
  • Save riccardopirani/f7fbd9bca476c0134c2c212c79a46d4c to your computer and use it in GitHub Desktop.
Save riccardopirani/f7fbd9bca476c0134c2c212c79a46d4c to your computer and use it in GitHub Desktop.
Ui generation with Future Flutter error
import 'dart:async';
import 'package:MyApp/Controller/Cantiere.dart';
import 'package:MyApp/Model/Cliente.dart';
import 'Utente.dart';
//Classe che rappresenta il cantiere
class Cantiere {
//Variabili del cantiere
String _nomeCantiere,
_Tipologia,
_Stato,
_DescrizioneEstesa,
_DataCreazione;
int _IdCantiere,_StatoFatturazione;
Cliente _c;
//Costruttore base
Cantiere(
this._c,
this._IdCantiere,
this._nomeCantiere,
this._DescrizioneEstesa,
this._Tipologia,
this._Stato,
this._StatoFatturazione,
this._DataCreazione);
//Funzione per la ricerca dei cantieri
static Future<List<Cantiere>> ricerca(Utente u, int IdCantiere, String NomeCantiere,
String RagioneSociale, bool isUtente) async =>
CantiereController.Ricerca(
u, IdCantiere, NomeCantiere, RagioneSociale, isUtente);
//Funzione che permette di recuperare il nome di un cantiere
String getNomeCantiere() {
return this._nomeCantiere;
}
//Funzione che permette di recuperare la ragione sociale di un cliente
String getRagioneSociale() {
return this._c.GetRagioneSociale();
}
//Funzione che permette di recuperare la data di creazione di un cantiere
String getDataCreazione() {
return this._DataCreazione;
}
}
flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ flutter: The following NoSuchMethodError was thrown building FutureBuilder(state: flutter: _FutureBuilderState#af0c6): flutter: The getter 'length' was called on null. flutter: Receiver: null flutter: Tried calling: length flutter: flutter: The relevant error-causing widget was: flutter: FutureBuilder flutter:
file:///Users/riccardo/Desktop/Progetti/Personali/MyAppFlutter/MyApp/lib/View/Home.dart:53:15 flutter: flutter: When the exception was thrown, this was the stack: flutter: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) flutter: #1
_TrainingScreenState.addAllListData (package:MyApp/View/Cantieri/cantieri_screen_view.dart:126:27) flutter: #2 _TrainingScreenState.initState (package:MyApp/View/Cantieri/cantieri_screen_view.dart:59:5) .. (package:flutter/src/widgets/framework.dart:4243:16) flutter: #72
Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5) flutter: #73 StatelessElement.update (package:flutter/src/widgets/framework.dart:4298:5) flutter: #74
Element.updateChild (package:flutter/src/widgets/framework.dart:2977:15) (package:flutter/src/scheduler/binding.dart:957:5) flutter: #86
_invoke (dart:ui/hooks.dart:259:10) flutter: #87 _drawFrame (dart:ui/hooks.dart:217:3) flutter: (elided 3 frames from package dart:async) flutter: flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════ flutter: Another exception was thrown: Each child must be laid out exactly once. flutter: Another exception was thrown: Updated layout information required for RenderErrorBox#34cf2 NEEDS-LAYOUT NEEDS-PAINT to calculate semantics. flutter: Another exception was thrown: NoSuchMethodError: The getter 'length' was called on null. flutter: Another exception was thrown: Each child must be laid out exactly once.MyApp
import 'package:flutter/material.dart' show Alignment, AnimatedBuilder, Animation, AnimationController, BorderRadius, BoxDecoration, BoxShadow, BoxShape, BuildContext, Column, Container, CrossAxisAlignment, EdgeInsets, Expanded, FadeTransition, FontWeight, Icon, Icons, Key, LinearGradient, MainAxisAlignment, Matrix4, Offset, Padding, Radius, Row, SizedBox, StatelessWidget, Text, TextAlign, TextStyle, Transform, Widget;
import 'package:MyApp/Model/Cantiere.dart';
import '../Home_theme.dart';
import 'package:MyApp/Utils/Style/color.dart';
//Classe che rappresenta la scheda di un cantiere
class SchedaCantieriView extends StatelessWidget {
final AnimationController animationController;
final Animation animation;
//Oggetto che rappresenta il cantiere
final Cantiere ctemp;
//Costruttore
const SchedaCantieriView({Key key, this.animationController, this.animation, this.ctemp})
: super(key: key);
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget child) {
return FadeTransition(
opacity: animation,
child: new Transform(
transform: new Matrix4.translationValues(
0.0, 30 * (1.0 - animation.value), 0.0),
child: Padding(
padding: const EdgeInsets.only(
left: 24, right: 24, top: 16, bottom: 18),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(colors: [
TemaApp.nearlyDarkBlue,
HexColor("#6F56E8")
], begin: Alignment.topLeft, end: Alignment.bottomRight),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
bottomLeft: Radius.circular(8.0),
bottomRight: Radius.circular(8.0),
topRight: Radius.circular(68.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: TemaApp.grey.withOpacity(0.6),
offset: Offset(1.1, 1.1),
blurRadius: 10.0),
],
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: TemaApp.fontName,
fontWeight: FontWeight.normal,
fontSize: 14,
letterSpacing: 0.0,
color: TemaApp.white,
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'Cantiere: '+ctemp.getNomeCantiere().toString()+' \n\nCliente: '+ctemp.getRagioneSociale()+'',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: TemaApp.fontName,
fontWeight: FontWeight.normal,
fontSize: 20,
letterSpacing: 0.0,
color: TemaApp.white,
),
),
),
SizedBox(
height: 32,
),
Padding(
padding: const EdgeInsets.only(right: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 4),
child: Icon(
Icons.timer,
color: TemaApp.white,
size: 16,
),
),
Padding(
padding: const EdgeInsets.only(left: 4.0),
child: Text(
''+ctemp.getDataCreazione() ,
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: TemaApp.fontName,
fontWeight: FontWeight.w500,
fontSize: 14,
letterSpacing: 0.0,
color: TemaApp.white,
),
),
),
Expanded(
child: SizedBox(),
),
Container(
decoration: BoxDecoration(
color: TemaApp.nearlyWhite,
shape: BoxShape.circle,
boxShadow: <BoxShadow>[
BoxShadow(
color: TemaApp.nearlyBlack
.withOpacity(0.4),
offset: Offset(8.0, 8.0),
blurRadius: 8.0),
],
),
//Bottone che permette l'accesso al cantiere
child: Padding(
padding: const EdgeInsets.all(0.0),
child: Icon(
Icons.arrow_right,
color: HexColor("#6F56E8"),
size: 44,
),
),
)
],
),
)
],
),
),
),
),
),
);
},
);
}
}
import 'dart:async';
import 'package:MyApp/Model/Utente.dart';
import 'package:MyApp/View/Cantieri/scheda_cantiere_view.dart'
show SchedaCantieriView;
import 'package:flutter/material.dart';
import 'package:MyApp/Model/Cantiere.dart';
import 'package:MyApp/utils/support.dart';
import '../Home_theme.dart';
//Variabile che rappresenta l'utente all'interno della view
Utente utemp;
//Questa map rappresenta i cantieri ricercati
List<Cantiere> cantieriricercati;
//La funzione di inizializzazione esegue l'init di tutti i valori che
//verranno utilizzati nella view
Future<bool> inizializzaValori() async {
await Future<dynamic>.delayed(const Duration(milliseconds: 100));
print("sono all'interno della funzione che fa l'init dei valori");
//Estrapolo i valori per inizializzare l'utente
var email = await Storage.leggi("Email");
var password = await Storage.leggi("Password");
var idutente = int.parse(await Storage.leggi("IdUtente"));
//Inizializzo l'utente
utemp = new Utente.init(idutente, email, password);
//Inizializzo i cantieri all'avvio della form
cantieriricercati = await Cantiere.ricerca(utemp, 0, "", "", false);
//Stampa dei valori dei cantieri ricercati
/*
for (int i = 0; i < cantieriricercati.length; i++) {
print("Cantieri ricercati -- Nome Cantiere: " +
cantieriricercati[i].getNomeCantiere());
}*/
return true;
}
class TrainingScreen extends StatefulWidget {
const TrainingScreen({Key key, this.animationController}) : super(key: key);
final AnimationController animationController;
@override
_TrainingScreenState createState() => _TrainingScreenState();
}
class _TrainingScreenState extends State<TrainingScreen>
with TickerProviderStateMixin {
Animation<double> topBarAnimation;
List<Widget> listViews = <Widget>[];
final ScrollController scrollController = ScrollController();
double topBarOpacity = 0.0;
@override
void initState() {
topBarAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController,
curve: Interval(0, 0.5, curve: Curves.fastOutSlowIn)));
addAllListData();
scrollController.addListener(() {
if (scrollController.offset >= 24) {
if (topBarOpacity != 1.0) {
setState(() {
topBarOpacity = 1.0;
});
}
} else if (scrollController.offset <= 24 &&
scrollController.offset >= 0) {
if (topBarOpacity != scrollController.offset / 24) {
setState(() {
topBarOpacity = scrollController.offset / 24;
});
}
} else if (scrollController.offset <= 0) {
if (topBarOpacity != 0.0) {
setState(() {
topBarOpacity = 0.0;
});
}
}
});
super.initState();
}
void addAllListData() {
const int count = 5;
listViews.add(
new TextFormField(
decoration: new InputDecoration(
labelText: "Ricerca Cantieri",
fillColor: Colors.white,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(25.0),
borderSide: new BorderSide(),
),
//fillColor: Colors.green
),
validator: (val) {
if (val.length == 0) {
return "Il campo non può essere vuoto";
} else {
return null;
}
},
keyboardType: TextInputType.emailAddress,
style: new TextStyle(
fontFamily: "WorkSansSemiBold",
),
),
);
//Aggiunta alla list della view corrispondente al cantiere
/* listViews.add(
SchedaCantieriView(
animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: widget.animationController,
curve:
Interval((1 / count) * 2, 1.0, curve: Curves.fastOutSlowIn))),
animationController: widget.animationController,
),
);*/
if (cantieriricercati.length != null) {
//Eseguo la ricerca dei cantieri
for (int i = 0; i < cantieriricercati.length; i++) {
print("\n Sono nel ciclo di for: " + i.toString());
//Aggiunta alla list della view corrispondente al cantiere
listViews.add(
SchedaCantieriView(
animation: Tween<double>(begin: 0.0, end: 1.0).animate(
CurvedAnimation(
parent: widget.animationController,
curve: Interval((1 / count) * 2, 1.0,
curve: Curves.fastOutSlowIn))),
animationController: widget.animationController,
ctemp: cantieriricercati[i],
),
);
}
}
}
Future<bool> getData() async {
await inizializzaValori();
await Future<dynamic>.delayed(const Duration(milliseconds: 100));
return true;
}
@override
Widget build(BuildContext context) {
return Container(
color: TemaApp.background,
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
getMainListViewUI(),
getAppBarUI(),
SizedBox(
height: MediaQuery.of(context).padding.bottom,
)
],
),
),
);
}
Widget getMainListViewUI() {
return FutureBuilder<bool>(
future: inizializzaValori(),
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (!snapshot.hasData) {
return const SizedBox();
} else {
return ListView.builder(
controller: scrollController,
padding: EdgeInsets.only(
top: AppBar().preferredSize.height +
MediaQuery.of(context).padding.top +
24,
bottom: 62 + MediaQuery.of(context).padding.bottom,
),
itemCount: listViews.length,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index) {
widget.animationController.forward();
return listViews[index];
},
);
}
},
);
}
Widget getAppBarUI() {
return Column(
children: <Widget>[
AnimatedBuilder(
animation: widget.animationController,
builder: (BuildContext context, Widget child) {
return FadeTransition(
opacity: topBarAnimation,
child: Transform(
transform: Matrix4.translationValues(
0.0, 30 * (1.0 - topBarAnimation.value), 0.0),
child: Container(
decoration: BoxDecoration(
color: TemaApp.white.withOpacity(topBarOpacity),
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(32.0),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: TemaApp.grey.withOpacity(0.4 * topBarOpacity),
offset: const Offset(1.1, 1.1),
blurRadius: 10.0),
],
),
child: Column(
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).padding.top,
),
Padding(
padding: EdgeInsets.only(
left: 16,
right: 16,
top: 16 - 8.0 * topBarOpacity,
bottom: 12 - 8.0 * topBarOpacity),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Cantieri',
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: TemaApp.fontName,
fontWeight: FontWeight.w700,
fontSize: 22 + 6 - 6 * topBarOpacity,
letterSpacing: 1.2,
color: TemaApp.darkerText,
),
),
),
),
/*SizedBox(
height: 38,
width: 38,
child: InkWell(
highlightColor: Colors.transparent,
borderRadius: const BorderRadius.all(
Radius.circular(32.0)),
onTap: () {},
child: Center(
child: Icon(
Icons.keyboard_arrow_right,
color: FintnessAppTheme.grey,
),
),
),
),*/
],
),
)
],
),
),
),
);
},
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment