Home > Enterprise >  Flutter expected a method getter setter or operator declaration
Flutter expected a method getter setter or operator declaration

Time:12-05

Can someone explain what is wrong with the code below? Cause I have the following error error image

Contact.fromMap(Map m) {
    identifier = m["identifier"];
    displayName = m["displayName"];
    givenName = m["givenName"];
    middleName = m["middleName"];
    familyName = m["familyName"];
    emails = (m["emails"] as List?)?.map((m) => Item.fromMap(m)).toList();
    phones = (m["phones"] as List?)?.map((m) => Item.fromMap(m)).toList();
    avatar = m["avatar"];
  }

String? identifier,
      displayName,
      givenName,
      middleName,
      prefix,
      suffix,
      familyName,
      company,
      jobTitle;
  String? androidAccountTypeRaw, androidAccountName;
  List<Item>? emails = [];
  List<Item>? phones = [];
  Uint8List? avatar;

Tried changing the code, but didn't help.

CodePudding user response:

What is the name of your class? It must be Contact.

CodePudding user response:

This is the whole code

import 'package:flutter_contacts/flutter_contacts.dart';
import 'dart:typed_data';
import 'package:flutter/services.dart';

import 'dart:async';
import 'package:collection/collection.dart';
import 'package:quiver/core.dart';


Future createUpdateContact(
  String name,
  String surname,
  String cellnumber,
  String email,
  String photo,
) async {
  String displayName = name   " "   surname;
  String phoneNumber = cellnumber;
  List<dynamic> phoneContacts = [];
  Uint8List bytes = (await NetworkAssetBundle(Uri.parse(photo)).load(photo))
      .buffer
      .asUint8List();

  if (await FlutterContacts.requestPermission()) {
    List<dynamic> contacts = await FlutterContacts.getContacts();  
    phoneContacts = contacts.map((c) => Contact.fromMap(c)).toList();
    
    
    try {
      dynamic contactToUpdate = matchContact(displayName, phoneNumber, phoneContacts);

      if (contactToUpdate == null) {
        final newContact = Contact()
          ..name.first = name
          ..name.last = surname
          ..phones = [Phone(cellnumber)]
          ..emails = [Email(email)]
          ..photo = bytes;
        await newContact.insert();
      } else {
        contactToUpdate.name.first = name;
        contactToUpdate.name.last = surname;
        contactToUpdate.phones = [Phone(cellnumber)];
        contactToUpdate.emails = [Email(email)];
        await contactToUpdate.update();
      }
    } catch (e) {
      print(e);
    }
  }
}

Contact matchContact(String displayName, String phoneNumber, List<dynamic> contacts) {
  return contacts.firstWhere((c) => c.displayName == displayName || c.phones.contains(phoneNumber), orElse: () => null);
}

 Contact.fromMap(Map m) {
    identifier = m["identifier"];
    displayName = m["displayName"];
    givenName = m["givenName"];
    middleName = m["middleName"];
    familyName = m["familyName"];
    emails = (m["emails"] as List?)?.map((m) => Item.fromMap(m)).toList();
    phones = (m["phones"] as List?)?.map((m) => Item.fromMap(m)).toList();
    avatar = m["avatar"];
  }

String? identifier,
      displayName,
      givenName,
      middleName,
      prefix,
      suffix,
      familyName,
      company,
      jobTitle;
  String? androidAccountTypeRaw, androidAccountName;
  List<Item>? emails = [];
  List<Item>? phones = [];
  Uint8List? avatar;
  • Related