Problem "Instance Member can’t be accessed using Static access." in a simple registration system

Asked

Viewed 22 times

0

I’m trying to create a registration system and for some reason I can’t access the "put" (add) method, located in the "users.Dart", on the screen where the entries are shown, the "cadastros.Dart".

The following error appears: 'Instance Member "put" can’t be accessed using Static access.'

cadastrals.Dart

import 'package:flutter/material.dart';
import 'package:projetofinal/components/usuario_tile.dart';
import 'package:provider/provider.dart';
import 'package:projetofinal/provider/users.dart';
import 'package:projetofinal/model/user.dart';

class cadastros extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final Users usuarios = Provider.of(context);

    return Scaffold(
      appBar: AppBar(
        title: Text('Usuarios Cadastrados'),
        actions: <Widget>[
          IconButton(
              onPressed: () {
                Users.put(User(  /*ERRO OCORRE NESSA LINHA */
                  id: '',
                  nome: 'teste',
                  email: 's',
                  telefone: 'w',
                  pfp: '',
                ));
              },
              icon: Icon(Icons.add))
        ],
      ),
      body: ListView.builder(
        itemCount: usuarios.count,
        itemBuilder: (cxt, i) => UserTile(usuarios.byIndex(i)),
      ),
    );
  }
}

Dart users.

import 'package:flutter/cupertino.dart';
import 'dart:math';
import 'package:projetofinal/data/dummy_users.dart';
import 'package:projetofinal/model/user.dart';

class Users with ChangeNotifier {
  final Map<String, User> _items = {...DUMMY_USERS};

  List<User> get all {
    return [..._items.values];
  }

  int get count {
    return _items.length;
  }

  User byIndex(int i) {
    return _items.values.elementAt(i);
  }

  void put(User user) {
    if (user == null) {
      return;
    }

    if (user.id != null &&
        !user.id.trim().isEmpty &&
        _items.containsKey(user.id)) {
      _items.update(
        user.id,
        (_) => User(
          id: user.id,
          nome: user.nome,
          email: user.email,
          telefone: user.telefone,
          pfp: user.pfp,
        ),
      );
    } else {
      //adicionar
      final id = Random().nextDouble.toString();

      _items.putIfAbsent(
        id,
        () => User(
          id: id,
          nome: user.nome,
          email: user.email,
          telefone: user.telefone,
          pfp: user.pfp,
        ),
      );
      notifyListeners();
    }
  }

//remover

  void remove(User user) {
    if (user != null && user.id != null) {
      _items.remove(user.id);
      notifyListeners();
    }
  }
}
  • Have you tried using usuarios in place of Users? Users is the class name... it would only make sense to call a method with the class name if it was static, hence the error. But to affirm with certainty only better understanding its use, what its intention, and where is the Provider above this point in the widgets tree.

  • That’s right Naulausky, thanks for your help!

No answers

Browser other questions tagged

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