0
votes
import '../models/activity_model.dart';
import 'package:flutter/foundation.dart';

   class Trip {
   String id;
   String city;
  String name;
  List<Activity> ;
  DateTime date;
  Trip({
    @required this.name,
    @required this.city,
    @required this.activities,
    @required this.date,
  }) : id = UniqueKey().toString();
}

trip_activityList.dart

import 'package:flutter/material.dart';
import '../../../models/activity_model.dart';

class TripActivityList extends StatelessWidget {
  final List<Activity> activities;
  TripActivityList({this.activities});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: activities.length,
        itemBuilder: (context, index) {
          return Text(activities[index].name);
        });
  }
}

trip_activities.dart

import 'package:flutter/material.dart';
import 'package:uvs_voyage/views/trip/widgets/trip_activity_list.dart';

import '../../../models/activity_model.dart';

class TripActivities extends StatelessWidget {
  final List<Activity> activities;
  TripActivities({this.activities});
  @override
  Widget build(BuildContext context) {
    return Container(
      child: DefaultTabController(
        length: 2,
        child: Column(
          children: <Widget>[
            Container(
              color: Colors.blue,
              child: TabBar(
                indicatorColor: Colors.blue[100],
                tabs: <Widget>[
                  Tab(
                    text: 'En cours',
                  ),
                  Tab(
                    text: 'Terminé',
                  ),
                ],
              ),
            ),
            Container(
              height: 600,
              child: TabBarView(
                physics: NeverScrollableScrollPhysics(),
                children: <Widget>[
                  TripActivityList(
                    activities: activities,
                  ),
                  TripActivityList(
                    activities: activities,
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

This is taking an unexpectedly long time.) /I/flutter (15602): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (15602): The following NoSuchMethodError was thrown building TripActivityList(dirty): I/flutter (15602): The setter 'length=' was called on null. I/flutter (15602): Receiver: null I/flutter (15602): Tried calling: length=0 I/flutter (15602): I/flutter (15602): The relevant error-causing widget was: I/flutter (15602): TripActivityList I/flutter (15602): file:///C:/Users/Mamadou%20Lamine%20Bouss/uvs_voyage/lib/views/trip/widgets/trip_activities.dart:35:19 I/flutter (15602): I/flutter (15602): When the exception was thrown, this was the stack: I/flutter (15602): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) I/flutter (15602): #1 TripActivityList.build (package:uvs_voyage/views/trip/widgets/trip_activity_list.dart:11:31) I/flutter (15602): #2 StatelessElement.build (package:flutter/src/widgets/framework.dart:4620:28) I/flutter (15602): #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) I/flutter (15602): #4 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #5 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #8 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #9 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #10 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #11 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14) I/flutter (15602): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #13 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14) I/flutter (15602): #14 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #15 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #16 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #17 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) I/flutter (15602): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #19 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) I/flutter (15602): #20 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) I/flutter (15602): #21 ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5) I/flutter (15602): #22 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) I/flutter (15602): #23 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)

1

1 Answers

1
votes

The list activities is possibly null when the Widget is instantiated. You need to guard against the case when data == null

 itemCount: activities!= null ? activities.length : 0,