Golam Sarwar Shakil
Golam Sarwar Shakil

Reputation: 175

Invalid reference to 'this' expression

I am trying to fetch data where the 'slug' parameter which needs to to be pass in the url which i have got from previous screen but i can't access it.

Error showing in this line:

 return BaseClient()
          .postData(url, 'filter/productByCategory?page=1&slug=${this.slug}', '');

Full Code:

  class CategoryDetails extends ConsumerWidget {
    CategoryDetails({this.categoryName = '', this.slug = ''});

    final String categoryName;
    final String slug;

    final productCategoryDetailsProvider = FutureProvider<dynamic>((ref) async {
      final String url = baseUrl;

      return BaseClient()
          .postData(url, 'filter/productByCategory?page=1&slug=${this.slug}', '');
    });

    @override
    Widget build(BuildContext context, ScopedReader watch) {
      return Scaffold(
        appBar: AppBar(
          title: Text(categoryName),
          iconTheme: IconThemeData(color: Colors.white),
          backgroundColor: Colors.purple,
        ),
        body: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                SizedBox(
                  height: 40,
                ),
                Text(
                  categoryName,
                  style: TextStyle(fontSize: 24, color: Colors.purple),
                ),
                Text(
                  slug,
                  style: TextStyle(fontSize: 24, color: Colors.purple),
                ),
                SizedBox(
                  height: 20,
                ),
                Container(
                  margin: EdgeInsets.all(20),
                  child: GridView.builder(
                    itemCount: 9,
                    physics: BouncingScrollPhysics(),
                    gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 180,
                        childAspectRatio: 3 / 4,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10),
                    shrinkWrap: true,
                    itemBuilder: (BuildContext ctxt, int index) => ProductCard(),
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }
  }

Upvotes: 0

Views: 1152

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 267474

With null safety, you could use the late keyword to fix that error:

Instead of

final productCategoryDetailsProvider = ...

use

late final productCategoryDetailsProvider = ...

Upvotes: 1

Benedict
Benedict

Reputation: 458

You are initialising categoryName and slug in the 'CategoryDetails' constructor. Why not leave it as:

CategoryDetails({this.categoryName, this.slug});

Upvotes: 0

Related Questions