NJ9
NJ9

Reputation: 3

"Error: NoSuchMethodError: method 'take' call of null

My main file '''

import 'dart:html';
import 'package:flutter/material.dart';

import 'random_words.dart';

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {



 @override
    Widget build(BuildContext context) {
     
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.purple[900]),
      home: RandomWords());
    
  }
}

'''

My random_words file '''

import 'dart:async';
import 'dart:html';

import 'package:flutter/material.dart';

class RandomWords extends StatefulWidget {
  @override
  RandomWordsState createState() => RandomWordsState();
}

class RandomWordsState extends State<RandomWords>{
  final _randomWordPairs = <WordPair>[];
  final _savedWordPairs = <WordPair>[];
  Widget _buildList () {
   return ListView.builder(
  padding: const EdgeInsets.all(16),
  itemBuilder: (context,item){
    if(item.isOdd) return Divider();

    final index = item ~/ 2;

    if(index>=_randomWordPairs.length){
     _randomWordPairs.addAll(generateWordPairs().take(10));
    }

    return _buildRow(_randomWordPairs[index]);
  }
);
  }
  Widget _buildRow(WordPair pair){
    final alreadySaved = _savedWordPairs.contains(pair);

    return ListTile(title: Text('pair.asPascalCase', style: 
    TextStyle(fontSize: 18.0),),
    trailing: Icon(alreadySaved ? Icons.favorite:
    Icons.favorite_border,
    color: alreadySaved ? Colors.red: null),
    onTap: (){
      setState(() {
        if(alreadySaved){
          _savedWordPairs.remove(pair);
          }else{
            _savedWordPairs.add(pair);
          }
      });
    },
    );
  }
  void _pushSaved() {
    Navigator.of(context).push(
    MaterialPageRoute(builder:(BuildContext context){
      final Iterable<ListTile> tiles =
      _savedWordPairs.map((WordPair pair){
        return ListTile(
          title: Text(pair.asPascalCase, style: TextStyle
          (fontSize: 16.0),
          ),
        );
      }); 
        final List<Widget> divided = ListTile.divideTiles
        (context: context,
        tiles: tiles
         ).toList();

         return Scaffold(appBar: AppBar(title: Text(
          'Saved WordPairs')), body: ListView(children: divided));
    }      
    )
    );
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('WordPair Generator'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.list),
            onPressed: _pushSaved)
        ],),
      body: _buildList());
      
  }

  generateWordPairs() {}
}
class WordPair {
  get asPascalCase => null;

  static random() {}
}

Im only getting this no such method error as of now. Im planning to make a word-pair app and any pair of your choice can be favourited. It can later be viewed from a list and unfavourited too. Am stuck on this. Any help is appreciated Got this from youtubehttps://www.youtube.com/watch?v=1gDhl4leEzA

Upvotes: 0

Views: 80

Answers (1)

TimMutlow
TimMutlow

Reputation: 305

The starting point is to find this issue and the error suggests that you are calling take() on a null value.

Having a quick search for "take", it looks like you simply haven't finished your build yet since you have generateWordPairs().take(); and generateWordPairs() {} with no body which returns nothing/null.

This isn't picked up at compile time because by not giving the generateWordPairs function a return value in its declaration, it is assumed to be dynamic type that may well have a take() function but the IDE can't tell one way or the other.

I would suggest declaring the generateWordPairs function fully (even if it doesn't have a body just yet) and your IDE will throw up some errors and warnings to help you along.

// For example
List<WordPair> generateWordPairs() {}

// Will show a "no return value"

Of course, it is best to continue working on the app so there aren't unfinished bits of code that will result in this kind of error.

I can heartily recommend Test-Driven Development (TDD) in flutter which gets you to write the tests first before you start developing logic code. I won't go into detail here as there are plenty of resources online but it really helps you structure your code and ensure your logic works as you'd expect without you having to run the app and debug it each time to find out why it doesn't work.

Intro to Unit Testing from the official docs: https://flutter.dev/docs/cookbook/testing/unit/introduction

Brief and to the point example of TDD in Flutter: https://itnext.io/test-driven-development-in-flutter-e7fe7921ea92

Best of luck with your app!

Upvotes: 1

Related Questions