StarLord
StarLord

Reputation: 35

Flutter: The method 'add' was called on null

I have created a list as follows:

List Links;

I use this function to add links:

Widget favoriteButton() {
    final _urlKey = GlobalKey<State>();
    return FutureBuilder<WebViewController>(
        key: _urlKey,
        future: _controller.future,
        builder: (BuildContext context,
            AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
              onPressed: () async {
                final String url = await controller.data.currentUrl();
                Links.add(url.substring(30));

                if (Links.isNotEmpty) {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => DemoApp(),
                    ),
                  );
                }
              },
              child: const Icon(Icons.add),
            );
          }
          return Container();
        });
  }

But when i call this function i'am getting an error:

Error: Unhandled Exception: NoSuchMethodError: The method 'add' was called on null.

Upvotes: 0

Views: 462

Answers (1)

Jigar Patel
Jigar Patel

Reputation: 5423

You need to initialize an empty List at the beginning.

List Links = [];

Upvotes: 2

Related Questions