Error :"Getlifecyclebase" not found. You need to call "Get.put(Getlifecyclebase())" or "Get.lazyPut(()=>Getlifecyclebase())"

Asked

Viewed 103 times

1

I’m using getx for state management and I’m having a mistake that says the following:

"Getlifecyclebase" not found. You need to call "Get.put(Getlifecyclebase())" or "Get.lazyPut(()=>Getlifecyclebase())"

Here’s the view:

import 'package:flutter/material.dart';
import 'package:flutter_application_stock/controllers/product_controller.dart';
import 'package:get/get.dart';
class HistoryScreen extends GetWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: GetX<ProductController>(
      init: Get.put(ProductController()),
      builder: (ProductController productController) {
        if (productController != null) {
          return Expanded(
            child: ListView.builder(
              itemCount: productController.produtos.length,
              itemBuilder: (_, index) {
                return Text(productController.produtos[index].nome);
              },
            ),
          );
        } else {
          return Text("Loading");
        }
      },
    )));
  }
}

The controller:

import 'package:flutter_application_stock/models/produto_model.dart';
import 'package:flutter_application_stock/services/database.dart';
import 'package:get/get.dart';
class ProductController extends GetxController {
  Rx<List<ProductModel>> productList = Rx<List<ProductModel>>();
  List<ProductModel> get produtos => productList.value;
  @override
  void onInit() {
    productList.bindStream(Database().productStream());
  }
}

A database:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_application_stock/models/produto_model.dart';
class Database {
  Firestore _firestore = Firestore.instance;
  Stream<List<ProductModel>> productStream() {
    return _firestore
        .collection("products")
        .snapshots()
        .map((QuerySnapshot query) {
      List<ProductModel> retVal = List();
      query.documents.forEach((element) {
        retVal.add(ProductModel.fromDocumentSnapshot(element));
      });
      return retVal;
    });
  }
}

the model:

import 'package:cloud_firestore/cloud_firestore.dart';
class ProductModel {
  String id;
  String nome;
  String embalagem;
  String categoria;
  ProductModel({this.id, this.nome, this.embalagem, this.categoria});
  ProductModel.fromDocumentSnapshot(DocumentSnapshot snapshot) {
    id = snapshot.documentID;
    nome = snapshot.data["nome"];
    embalagem = snapshot.data["embalagem"];
    categoria = snapshot.data["categoria"];
  }
}

I’d appreciate any help

1 answer

0

I believe it is even bad practice to register Singleton within init: of GetX(), init already needs an instance ready to use it and not that it still needs to register it.

init: Get.put(ProductController())

Usually you register before somewhere, for example in main, or using bindings, where you automatically register your dependencies.

Experient to call the code below in main.Dart to see if it gives the same error.

Get.put(ProductController())
// ou
Get.lazyPut(() => ProductController());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.