AnupKumarPanwar / swipe_cards

Tinder like swipe cards for flutter.
https://pub.dev/packages/swipe_cards
BSD 3-Clause "New" or "Revised" License
52 stars 47 forks source link

The getter 'width' was called on null. #2

Open subhendukundu opened 3 years ago

subhendukundu commented 3 years ago

This L287 throwing an error of The getter 'width' was called on null.

Is there a way to fix this?

List<SwipeItem> _swipeItems = [];

final MatchEngine matchEngine = new MatchEngine(
    swipeItems: edges.map((edge) {
  final post = Post.fromJson(edge);
  final swipeItem = SwipeItem(
    content: post,
    likeAction: () {
      launchURL(post.node.slug);
    },
  );
  _swipeItems.add(swipeItem);
  return swipeItem;
}).toList());
return MyHomePage(
  matchEngine: matchEngine,
  swipeItems: _swipeItems,
);

Using it as follows. Then in MyHomePage

Scaffold(
      appBar: _buildAppBar(),
      body: Center(
        child: Container(
          constraints: BoxConstraints(
            minWidth: 300,
            maxWidth: 500,
            maxHeight: 730,
          ),
          padding: EdgeInsets.all(10),
          child: SwipeCards(
            matchEngine: matchEngine,
            itemBuilder: (BuildContext context, int index) {
              return ProfileCard(
                post: swipeItems[index].content,
              );
            },
            onStackFinished: () {
              print('finished');
            },
          ),
        ),
      ),
      bottomNavigationBar: _buildBottomBar(),
);
Frander commented 3 years ago

I have the same issue

ekgjessing commented 3 years ago

Same here

ryo12882 commented 3 years ago

I have the same issue too. Does anyone find a way to fix this?

alexboulay commented 3 years ago

Same issue

AnupKumarPanwar commented 3 years ago

@subhendukundu Is the app crashing due to this exception? I can see this in the logs but don't see any effect on the app. Though to avoid this exception, I've used null aware access on anchorBounds width and height.

anchorBounds?.width
anchorBounds?.height
aksagarwal42 commented 3 years ago

@AnupKumarPanwar I've stored some data on a localhost monogdb database and am getting the data from there to render it on my explorer page. While running the app, it sometimes gives an error of "NoSuchMethodError: : Getter Length called on null", not always.

Initially I thought importing the non-null safe version would solve the issue but it still persists. I have enclosed my code below, please tell me how to solve it.

Here, userExplorer is of the type List where Explorer is a custom class where the data from snapshot is stored.

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
// ignore: import_of_legacy_library_into_null_safe
import 'package:swipe_cards/swipe_cards.dart';
import 'package:tinder_clone/models/explorer.dart';
import 'package:tinder_clone/provider/explorer_profile.dart';

class ExplorerPage extends StatefulWidget {
  final String email;
  ExplorerPage({required this.email});
  @override
  _ExplorerPageState createState() => _ExplorerPageState();
}

class _ExplorerPageState extends State<ExplorerPage> {
  MatchEngine _matchEngine = MatchEngine();
  List<SwipeItem> _swipeItems = [];

  getLength() async {
    var userExplorer =
        await ExploreProfileProvider().getExplorerProfile(widget.email);
    //  for (int i = 0; i < userExplorer.length; i++) {
    //    _swipeItems.add(
    //     SwipeItem(
    //       likeAction: () {},
    //       nopeAction: () {},
    //     ),
    //   );
    // }
    for (var i in userExplorer) {
      _swipeItems.add(SwipeItem(
        likeAction: () {
          print('Right Swipe ${i.name}');
        },
        nopeAction: () {
          print('Left Swipe ${i.name}');
        },
      ));
    }
    _matchEngine = MatchEngine(swipeItems: _swipeItems);
  }

  @override
  void initState() {
    super.initState();
    getLength();
  }

  @override
  Widget build(BuildContext context) {
    final availableHeight = MediaQuery.of(context).size.height -
        AppBar().preferredSize.height -
        MediaQuery.of(context).padding.top -
        MediaQuery.of(context).padding.bottom;
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: FutureBuilder<List<Explorer>>(
              future: ExploreProfileProvider().getExplorerProfile(widget.email),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Column(
                    children: [
                      SwipeCards(
                        matchEngine: _matchEngine,
                        itemBuilder: (BuildContext context, int index) {
                          return Stack(
                            alignment: Alignment.bottomLeft,
                            children: [
                              Container(
                                width: MediaQuery.of(context).size.width,
                                height: availableHeight * 0.85,
                                child: snapshot.data![index].image != ""
                                    ? Image.memory(
                                        base64Decode(
                                            snapshot.data![index].image),
                                        fit: BoxFit.cover)
                                    : Image.asset(
                                        'assets/images/img_1.jpeg',
                                        fit: BoxFit.cover,
                                      ),
                              ),
                              Column(
                                mainAxisAlignment: MainAxisAlignment.end,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Container(
                                    child: Text(
                                      '${snapshot.data![index].name}  25',
                                      style: GoogleFonts.robotoSlab(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 20.0,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    margin: EdgeInsets.only(bottom: 10),
                                    child: Row(
                                      children: [
                                        Icon(Icons.location_on_outlined),
                                        Container(
                                          margin: EdgeInsets.only(left: 10),
                                          child:
                                              Text(snapshot.data![index].city),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          );
                        },
                        onStackFinished: () {
                          // print(_swipeItems);
                          print('Finished');
                        },
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          IconButton(
                            icon: Image.asset(
                              'assets/icons/close_icon.png',
                              scale: 0.7,
                            ),
                            padding: EdgeInsets.all(0),
                            // iconSize: 45,
                            onPressed: () {
                              _matchEngine.currentItem.nope();
                            },
                          ),
                          IconButton(
                            icon: Image.asset(
                              'assets/icons/like_icon.png',
                              scale: 0.7,
                            ),
                            // iconSize: 45,
                            onPressed: () {
                              _matchEngine.currentItem.like();
                            },
                          ),
                        ],
                      ),
                    ],
                  );
                } else if (snapshot.hasError) {
                  print(snapshot.error);
                  return Text('null');
                } else {
                  return Center(
                    child: CircularProgressIndicator(
                      color: Colors.white,
                    ),
                  );
                }
              }),
        ),
      ),
    );
  }
}

// class Content {
//   final String text;
//   final Color color;

//   Content({required this.text, required this.color});
// }